开发者

obtaining the results from one function and inputing it into another?

开发者 https://www.devze.com 2023-03-26 13:54 出处:网络
I have a class and a function and what I want to do is to \"return\" the value and input into another function.

I have a class and a function and what I want to do is to "return" the value and input into another function.

Class test1 {

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);

    $red = $base_result开发者_如何转开发s['red'];

}

public function c($red) {
$runsecond_query = "SELECT * test2db where $color = '{$red}'";
// write additional code

}

OK, so really what I would LIKE to do is to get the results from function "a" and input the result in function "c". I hope that makes sense. Thanks to anyone in advance.


Since both functions are members of the same class, you can create a class property to store them:

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
    }
}


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

c(a($x));

or, if you want it to be a little more legible:

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

精彩评论

暂无评论...
验证码 换一张
取 消