0

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

3 Answers 3

10

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;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Is he actually wanting to know if those variables exist?
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.
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.
thanks a lot guys.... I have finally got the complete solution to exactly what i wanted....thanks
1

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

        }

4 Comments

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
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 php.net site to better understand construction you are using here.
thanks a lot.. for the quick reply ... but i am basically trying to send a few parameters to the function and then extract 'n' no. of fields, from 'n' no. of rows & return all the values in an array... So that i can reuse this function whenever I want to extract data from my database, by simply calling the function along with the parameters....
I think the addition which @Gumbo made in his post after your update can significantly help you.
0

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().

1 Comment

Remove the parenthesis in $myArray().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.