Question

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

Was it helpful?

Solution

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

        }

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top