1

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!

2
  • 2
    You don't need to use for to have a counter Commented Oct 14, 2011 at 14:59
  • Just out of curiosity, why did someone downvote this question? Commented Oct 14, 2011 at 15:05

3 Answers 3

4

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;
}
8
  • Wow. That is such an awesome solution! So simple I didn't think of it! Thanks a lot!!! Commented Oct 14, 2011 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! Commented Oct 14, 2011 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 Commented Oct 14, 2011 at 15:10
  • Yeah,, though after the explanation from Jeremy, I realised that might even be a better solution: even more simple! Commented Oct 14, 2011 at 15:11
  • @laarsk, that is fine. Although, I wouldn't do it that way, for the reasons Jonathan Kuhn mentioned. Commented Oct 14, 2011 at 15:22
0

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
}
7
  • But how would I have it grab only one row? Thanks for the post, but I don't fully understand what you are saying.. Commented Oct 14, 2011 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. Commented Oct 14, 2011 at 15:06
  • ooh ok, So that would even be an easier solution than the one Brad provided? Commented Oct 14, 2011 at 15:09
  • Sure. No extra variable required. Commented Oct 14, 2011 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. Commented Oct 14, 2011 at 15:18
-1
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

1
  • fixed it. $row[$i] should probably also be just $row with $i as the count of the current row. Commented Oct 14, 2011 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.