easier way to get counter from while loop?
I have the following:
$counter = 1;
while($row= mysql_fetch_assoc($result)) {
$counter2 = $counter++;
开发者_开发百科echo($counter2 . $row['foo']);
}
Is there an easier way to get 1,2,3 etc for each result or is this the best way?
Thanks
You don't need $counter2. $counter++ is fine. You can even do it on the same line as the echo if you use preincrement instead of postincrement.
$counter = 0;
while($row= mysql_fetch_assoc($result)) {
echo(++$counter . $row['foo']);
}
I know it's not exactly what you have asked for - but why don't you simply use a for-loop instead of while?
for ($i = 0; $row = mysql_fetch_assoc($result); ++$i) {
echo $i . $row['foo'];
}
精彩评论