Jump to content

[SOLVED] loop through array backwards


jonaHill87

Recommended Posts

Hey all. I'm trying to loop through an array backwards but can't get it right. I'm getting my data through mysql_fetch_row and using mysql_num_rows to get the number of rows. I know how to loop forward through my fields but not backwards. Here's my foward loop for my first field. I'd like to loop this feild backwards. I'm guessing I need to use a for loop as well but I can't figure it out.

 

$query = "SELECT * FROM Table";

$result = mysql_query($query);

$num = mysql_num_rows($result);

 

while ($data = mysql_fetch_row($result)) {

echo $data[0];

}

 

Link to comment
https://forums.phpfreaks.com/topic/114956-solved-loop-through-array-backwards/
Share on other sites

This one is easy :D just put a desc value in your query. I'm pretty sure that it will just order the table backwards :D

 

<?php
$query = "SELECT * FROM Table ORDER BY row DESC";
$result = mysql_query($query);
$num = mysql_num_rows($result);

while ($data = mysql_fetch_row($result)) {
echo $data[0];
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.