2011-08-08 36 views
1

我有一個類和一個函數,我想要做的是「返回」的值和輸入到另一個函數。好吧,我真正想要做的就是從函數「a」得到結果並在函數「c」中輸入結果。我希望這是有道理的。感謝任何人提前。從一個函數中獲取結果並將其輸入到另一個函數中?

+1

對於使用的示例,它可能會更容易在MySQL中使用JOIN子句,這隻會節省正在使用的函數的數量和查詢的數量 –

回答

1

因爲這兩個功能都是相同類的成員,你可以創建一個類的屬性來存儲它們:

Class test1 { 

    // Private property to hold results 
    private $last_result; 

    public function a($x) { 
     $runquery = "Select * FROM testdb where color_id = '{$x}'"; 
     $result = mysql_query($runquery) or die(mysql_error()); 
     $base_results = mysql_fetch_array($result); 

     // Store your result into $this->last_result 
     $this->last_result = $base_results['red']; 
    } 

    public function c() { 
     $runsecond_query = "SELECT * test2db where $color = '{$this->last_result}'"; 
     // write additional code 
    } 
} 
+0

非常感謝你們!這是偉大的 – GGcupie

+0

我得到這個錯誤:當不在對象上下文中使用$ this - 任何想法如何解決? – GGcupie

+0

你是否正在實例化你的類'test1'?像:'$ t = new test1(); $ T-> A($ x)的; $ T-> C();' –

1
function a($x) { 
    .... 
    return $red; 
} 

c(a($x)); 

,或者,如果你希望它是一個小更清晰:

$red = a($x); 
c($red); 
+0

NEVERMIND我解決了它 – GGcupie

相關問題