在这个循环中存在任何更漂亮的选择吗?

时间:2009-06-01 13:36:05

标签: php pointers loops

$i=0;
while ($row=mysql_fetch_assoc()) {
if ($i==0) echo "First"
$i++;
}

直接访问mysqli指针?像Iterator这样的php类?

感谢。

5 个答案:

答案 0 :(得分:1)

我会用这个:

if ($row = mysql_fetch_assoc()) {
    // process first item
    while ($row = mysql_fetch_assoc()) {
        // process following items
    }
}

答案 1 :(得分:1)

$first = true;
while ($row=mysql_fetch_assoc()) {
   if ($first) echo "First"
   $first = false;
}

答案 2 :(得分:0)

如果你试图将第一个元素用于特殊的东西,那么可能是:

$row=mysql_fetch_assoc();
//do stuff to first row
do {
    //do stuff to all rows (including the first)
} while ($row=mysql_fetch_assoc());

否则我不知道问题是什么,我不是一个PHP人......

答案 3 :(得分:0)

没有任何方法可以随时确切地知道位置?

例如java iterator实现next或hasNext(),如果!hasNext()项是最后一个。

答案 4 :(得分:0)

好吧,如果我理解你的问题,你想要第一行,然后迭代其他行:

$row = mysql_fetch_assoc(); // $row contains first row

while ($row = mysql_fetch_assoc()) { // loop through the rest of the rows

}