0

I have this code in my php and would like to understand it better:

while($row = mysql_fetch_array($result)){
 echo $row['name'];
 }

How can I turn this into a "for" loop? I tried, but I didn't know how to reference the correct row I wanted...$row[$i]['name'] did not work...

EDIT - The goal is to reverse the echo of the output. So the results are the other way...I only know how to do this with for loops using $i-- –

3
  • That's hardly possible...why would you want that? Commented Feb 28, 2011 at 9:04
  • The goal is to reverse the echo of the output. So the results are the other way...I only know how to do this with for loops using $i-- Commented Feb 28, 2011 at 9:06
  • 1
    The more easiest way to reverse the output is to use ORDER BY with or without DESC in your sql query Commented Feb 28, 2011 at 9:29

6 Answers 6

3

Retrieve your number of rows using mysql_num_rows, then you can use a for-loop:

$count = mysql_num_rows($result);
for ($i=0; $i<$count; $i++)
{
  $row = mysql_fetch_array($result);
  echo $row['name'];
}

I'd use "mysql_fetch_assoc" for this one.

If you really need to use "[$i]['name']", you need to transfer the rows to an array first:

$rows = array();
while($row = mysql_fetch_array($result))
{
  $rows[] = $row;
}

Now you can access e.g. $row[0]['name'] using a for-loop, if you really need to.

EDIT:

The goal is to reverse the echo of the output.

To do that you can fetch all rows into an array (using mysq_fetch_assoc and the above while-loop to build the array), use array_reverse to reverse the order and then a foreach-loop to echo the elements. Still, no for-loop needed ;)

Sign up to request clarification or add additional context in comments.

Comments

3
for(;$row = mysql_fetch_array($result);) {
    echo $row['name'];
}

But why?

Comments

2

Have you tried something like this:

for ($row = mysql_fetch_array($result); $row; $row = mysql_fetch_array($result)) {
     echo $row['name'];
} 

In this approach you do not need to use $i variable.

Comments

2
for (;;) {
    if (!($row = mysql_fetch_array($result))) {
        break;
    }
    echo $row['name'];
}

Seriously though, you can't use a for loop here. mysql_fetch_array only gives you the next row as long as there are rows. As you correctly said, there's no index to refer to, hence not much use of looping over an index.

The goal is to reverse the echo of the output.

You do this in your query with an ORDER BY ... DESC/ASC clause.

Comments

0

The trick in this while construct is that the iteration stops when the assignment “fails”, i.e. mysql_fetch_array returns false as there are no (further) rows and thus the assignment returns false too.

To use a for instead of this while construct, you could use mysql_result instead of mysql_fetch_array to get a specific row from the result set:

for ($i=0, $n=mysql_num_rows($result); $i < $n; $i++) {
    $row = mysql_result($result, $i);
}

Comments

0

You have to set the order using database.

select * from table ORDER BY field DESC

Comments

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.