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!

share|improve this question

74% accept rate
2  
You don't need to use for to have a counter – Damien Pirsy Oct 14 '11 at 14:59
Just out of curiosity, why did someone downvote this question? – Brad Oct 14 '11 at 15:05
feedback

3 Answers

up vote 4 down vote accepted

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;
}
share|improve this answer
Wow. That is such an awesome solution! So simple I didn't think of it! Thanks a lot!!! – laarsk Oct 14 '11 at 15:03
I'm definitly going with this solution, its just perfect! I'm also stunned by the speed you all answerred my question, thank you all so much! – laarsk Oct 14 '11 at 15:08
@laarsk, No problem. If this worked for you, you should accept it as the answer by clicking the check mark to the left. You can read more about this here: stackoverflow.com/faq – Brad Oct 14 '11 at 15:10
Yeah,, though after the explanation from Jeremy, I realised that might even be a better solution: even more simple! – laarsk Oct 14 '11 at 15:11
@laarsk, that is fine. Although, I wouldn't do it that way, for the reasons Jonathan Kuhn mentioned. – Brad Oct 14 '11 at 15:22
show 4 more comments
feedback

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
}
share|improve this answer
But how would I have it grab only one row? Thanks for the post, but I don't fully understand what you are saying.. – laarsk Oct 14 '11 at 15:05
mysql_fetch_array fetches one row (or returns false) every time you run it. If you run it outside a loop, it just fetches the next row from the set. The first time, that means it fetches the first row. So if you run it outside the loop at the beginning, it's fetching the first row. – Jeremy Smyth Oct 14 '11 at 15:06
ooh ok, So that would even be an easier solution than the one Brad provided? – laarsk Oct 14 '11 at 15:09
no, it is not easier nor solution – Your Common Sense Oct 14 '11 at 15:11
1  
depending on the end result of what you are trying, you would be duplicating code in both the #process first... and process second... sections. Also if the query returns 0 rows (which is always possible) the first $row will be false so you need to check for that where the while will skip over the loop when 0 rows with no extra checking. Just a few ideas off the top of my head. – Jonathan Kuhn Oct 14 '11 at 15:18
show 5 more comments
feedback
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

share|improve this answer
fixed it. $row[$i] should probably also be just $row with $i as the count of the current row. – Jonathan Kuhn Oct 14 '11 at 15:08
shit forgot to change the function thanks – Your Common Sense Oct 14 '11 at 15:09
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.