>我有以下内容:
$counter = 1;
while($row= mysql_fetch_assoc($result)) {
$counter2 = $counter++;
echo($counter2 . $row['foo']);
}
有没有更简单的方法可以为每个结果获得 1,2,3 等,或者这是最好的方法?
谢谢
你不需要 $counter 2。 $counter++很好。 如果您使用前增量而不是后增量,您甚至可以在与回显相同的行上执行此操作。
$counter = 0;
while($row= mysql_fetch_assoc($result)) {
echo(++$counter . $row['foo']);
}
我知道
这不是你所要求的 - 但你为什么不简单地使用 for 循环而不是 while?
for ($i = 0; $row = mysql_fetch_assoc($result); ++$i) {
echo $i . $row['foo'];
}