tags:

views:

15

answers:

2

Hey guys I have an assigned array from mysql results and I simply want to number them starting at one. Does anyone know how to do this?

 while ($row=mysql_fetch_assoc($query)){
$out[] = array("ASSIGNED INTEGER", $row['total']);        
 }
+1  A: 
$i = 0;
while ($row=mysql_fetch_assoc($query)){
    $out[] = array($i++, $row['total']);        
}
Ivo Sabev
You should do ++$i or $i = 1 to start the numbering at 1.
webbiedave
+1 thanks ivo appreciate it
Scarface
+2  A: 

You'll have to use a variable as a counter, to keep track of the line you're on :

$counter = 1;
while ($row=mysql_fetch_assoc($query)){
    $out[] = array($counter, $row['total']);
    $counter++;
}


Or, if you want your resulting $out array have results indexed from 1, instead of 0, you could use something like this to set the index yourself :

$counter = 1;
while ($row=mysql_fetch_assoc($query)){
    $out[$counter] = $row['total'];
    $counter++;
}

Or any idea derived from this.

Pascal MARTIN
+1 thanks pascal, appreciate it
Scarface
You're welcome :-) Have fun !
Pascal MARTIN