开发者

replace while ($row=mysql_fetch_array($res)) loop with for loop

I was wondering how I could replace this loop: while ($row=mysql_fetch_array($res)) by an for loop.

I want to know, because on my 开发者_运维百科website, I want to show the data on my website, but the first row needs a different layout as the rest.

So what I would like to have, is something like this:

$row[0]

for ($i = 1; $i <= mysql_num_rows($result); $i++) {
$row[$i]
}

I tried a bit of searching here, but couldnt find something that seemed logical to me: most examples I saw would execute a new sql query to the server, which isnt very good for the speed, is it?

Please let me know!


I wouldn't do it that way. I would do something like this:

$first=true;
while ($row=mysql_fetch_array($res)) {
    if ($first) {
        //Extra formatting or whatever you need to do can go here
    }
    //Other code goes here
    $first=false;
}


Every time you run mysql_fetch_array it fetches the next row anyway, so you could just run it once outside your loop, and then have the loop go through the remaining rows.

What's not happening is a mysql_data_seek, which would cause it to restart at the top, but you're not doing that!

Example

$row = mysql_fetch_array($res);
# process first row here

while ($row = mysql_fetch_array($res)) {
    # process second and subsequent rows here
}


for ($i = 1; $row = mysql_fetch_row($result); $i++) {
  $row[$i]
}

of course it is literal, but totally useless answer.
To show data on your website, you should use a template.
So, you have to get your data into array first anyway

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜