本文介紹了從while循環中獲取計數器的更簡單方法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有以下幾點:
$counter = 1;
while($row= mysql_fetch_assoc($result)) {
$counter2 = $counter++;
echo($counter2 . $row['foo']);
}
是否有更簡單的方法可以為每個結果獲得 1、2、3 等,或者這是最好的方法?
Is there an easier way to get 1,2,3 etc for each result or is this the best way?
謝謝
推薦答案
你不需要 $counter2.$counter++ 很好.如果您使用 preincrement 而不是 postincrement,您甚至可以在與 echo 相同的行上執行此操作.
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']);
}
這篇關于從while循環中獲取計數器的更簡單方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!