michaelmcgurk
michaelmcgurk

Reputation: 6509

Create new variable to store each result during While loop

I have a while loop that loops through 3 results and echo's these out in a list. It will always be 3 results.

Here is my current PHP:

while($row = sqlsrv_fetch_array($res))
{

    echo "<li>".$row['SessionValue']."</li>";
    // prefer to store each value in its own variable

}

However I'd like to store the $row['SessionValue'] value in each loop in a new variable.

So....

first loop: $i0 = $row['SessionValue'];

second loop: $i1 = $row['SessionValue'];

third loop: $i2 = $row['SessionValue'];

How would I achieve this with PHP?

Many thanks for any pointers.

Upvotes: 1

Views: 7469

Answers (3)

Al Mobarmij
Al Mobarmij

Reputation: 36

You can do this have SUM of all value:

$total = array();
while($row = sqlsrv_fetch_array($res))
{
   $total[] = $row["SessionValue"]
}  $sumAll = array_sum($total);

Upvotes: 1

Stefan
Stefan

Reputation: 3900

$lst_count = array();
while($row = sqlsrv_fetch_array($res))
  $lst_count[] = $row["SessionValue"];

Upvotes: 1

Niko
Niko

Reputation: 26730

You just need another variable that gets incremented:

$count = 0;
while($row = sqlsrv_fetch_array($res))
{
    ${i.$count++} = $row['SessionValue'];
}

Upvotes: 1

Related Questions