views:

350

answers:

3

I want to return a set of values from function till the point they exist.... for example....

function abc($i="3"){

    for($a=1;$a<=$i;$a++) {
        $name='t'.$i;
        $$name = "ae".$a;
    }
    //now i am returning values 
    return array($t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10);
    //but i only want to return $t1,$t2,$t3 depending on $i
}

Thanks....

@therefromhere I am also creating an array in the loop , I'll paste the original code so that you can understand it in a better way

function extracting_comments($table, $fields,$condition,$order,$limit){
  $query="SELECT ".$fields."
    FROM ".$table."
    WHERE ".$condition."
    ORDER BY ".$order."
    LIMIT ".$limit." ";
  if($stmt = $this->conn->prepare($query)) {
   $stmt->execute();
   $row = array_pad(array(), $stmt->field_count, '');
   $params = array();
    foreach($row as $k=>$v) {
      $params[] = &$row[$k];
      echo $params[0];
    }
   call_user_func_array(array($stmt,'bind_result'),$params);
   $i=0;
   while($stmt->fetch()) {
   $i++;
   $name='t'.$i;
   $$name = array();
   foreach ($row as $b=>$elem) {
   $atul[$b]=$row[$b];
   }
   $$name=$atul;
   }
   return array($t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10);
   $stmt->close();
  }

 }

now their are only 5 rows of data so their is no point returning $t6,$t7,$t8,$t9,$t10 and i want to fix it ,and i am calling the function using

$extract=extracting_comments($table, $fields,$condition,$order,$limit);

please help...thanks

+10  A: 

Just build the array in your for loop:

function abc($i=3) {
    $array = array();
    for ($a=1; $a<=$i; $a++) {
        $array[] = "ae".$a;
    }
    return $array;
}


After you edited your question an revealed us your actual problem, see here my proposal:

function extracting_comments($table, $fields, $condition, $order, $limit) {
    $retVal = array();
    $query = "SELECT ".$fields."
        FROM ".$table."
        WHERE ".$condition."
        ORDER BY ".$order."
        LIMIT ".$limit." ";
    if ($stmt = $this->conn->prepare($query)) {
        $stmt->execute();
        $row = array_pad(array(), $stmt->field_count, '');
        call_user_func_array(array($stmt, 'bind_result'), $row);
        while ($stmt->fetch()) {
            $retVal[] = $row;
        }
        $stmt->close();
        return $retVal;
    }
}
Gumbo
Is he actually wanting to know if those variables exist?
Jonathan Sampson
But the comment in his code says “*but i only want to return $t1,$t2,$t3 depending on $i*”. And since those variables are just created in the `for` loop, he could just store the values they contain instead.
Gumbo
It was his "till the point they exist" comment that confused me. Like maybe he was willing to pass 19 iterations, but only wants to go as far as he's able to and still remain tied to variables that actually exist. So if $a1 - $a12 exist, the loop will only include a max of $a12.
Jonathan Sampson
thanks a lot guys.... I have finally got the complete solution to exactly what i wanted....thanks
halocursed
A: 

It'd be cleaner to build the array as you go along, then you wouldn't need the temporary variables:

function abc($i="3") {
  $myArray = array();
  for($a=1;$a<=$i;$a++) {
    $myArray[] = "ae" . $a;  // add new values to the end of the array
  }

  return $myArray;
}

If you want to check if the variables exist (and are not null), use isset().

therefromhere
Remove the parenthesis in `$myArray()`.
Gumbo
Whoops, thanks.
therefromhere
+1  A: 

I believe this will help you. You have very complicated code with a lot of side effects and bug, you'd better to consider to redisgn it. Also putting statements after return will no have any effect, since it wouldn't be invoked.

function extracting_comments($table, $fields,$condition,$order,$limit){
                $query="SELECT ".$fields."
                                FROM ".$table."
                                WHERE ".$condition."
                                ORDER BY ".$order."
                                LIMIT ".$limit." ";
                if($stmt = $this->conn->prepare($query)) {
                        $stmt->execute();
                        $row = array_pad(array(), $stmt->field_count, '');
                        $params = array();
                                foreach($row as $k=>$v) {
                                  $params[] = &$row[$k];
                                  echo $params[0];
                                }
                        call_user_func_array(array($stmt,'bind_result'),$params);
                        $i=0;
                        $result = array();
                        while($stmt->fetch()) {
                        $i++;
                        foreach ($row as $b=>$elem) {
                        $atul[$b]=$row[$b];
                        }
                        $result[]=$atul;
                        }
                        $stmt->close();
                        return $result;
                }

        }
Artem Barger
thanks a lot......artem barger problem solved for good
halocursed
hey Artem.... now I've changed my code acc. to your answer is it now bug free, or is their a better to do this ....if so please point me to the right direction... I hope you know what i am trying to with this function
halocursed
To be honest, I'm not completely sure what exactly you are trying to do here. But it look like you've produced a lot of redundant code. Probably it's worth spending some time reading http://php.net site to better understand construction you are using here.
Artem Barger
halocursed
I think the addition which @Gumbo made in his post after your update can significantly help you.
Artem Barger