tags:

views:

30

answers:

3

After I retrieved mysql result from db, I wish to add row number value '1' '2' '3' etc to each results[].

if($products)
{
    while($row = mysql_fetch_array($products)){
    $results[] = $row;
}
+1  A: 

If you mean that you'd like to start the array keys from 1, you could use a counter variable:

$counter = 1;
while ($row = mysql_fetch_array($products) {
  $results[$counter] = $row;
  $counter++;
}
Jonathan Sampson
+1  A: 

Bear in mind that:

while ($row = mysql_fetch_array($products)) {
  $results[] = $row;
}

will key each result starting from 0 so if you do:

foreach ($results as $k => $v) {
  // $k = 0, 1, 2, ...
}

You can explicitly set the key instead:

$i = 1;
while ($row = mysql_fetch_array($products)) {
  $results[$i++] = $row;
}

or you can add that number to the row itself if you wish:

$i = 1;
while ($row = mysql_fetch_array($products)) {
  $row['row_number'] = $i++;
  $results[] = $row;
}
cletus
A: 

Thank to both of you for the tips, using your code and modify, I got it work within my Flex application.

$row[] = $i++;
proyb2