Jump to content

ORDER BY in a loop?


m4x3vo

Recommended Posts

<?php
for ($counter = 1; $counter <= 139; $counter++)
{
$query = "SELECT * FROM user_ach WHERE user_id = $cookie AND ach_id = $counter";
$result1 = mysql_query($query);
$res = mysql_fetch_assoc($result1);
$ach_id = $res['ach_id'];
if($ach_id != null) {
$query2 = "SELECT * FROM ach WHERE id = $ach_id";
$result2 = mysql_query($query2);
$res = mysql_fetch_assoc($result2);
echo "<tr><td><img src='$res[image]'></img><br>$res[name]</td><td>$res[method]</td><td>$res[points]</td><td>$res[cat]</td><td>$res[status]</td></tr>";
echo "<br>";
}
}
?>

 

 

I have a loop like this, how can I use an ORDER BY to sort the results in the table..

Link to comment
https://forums.phpfreaks.com/topic/161605-order-by-in-a-loop/
Share on other sites

Anyone I want to sort by the cat column.

 

First off, why are you using a counter?  Just put an extra condition that takes care of this.  Second, why can't you just sort by adding ORDER BY cat to your query?

 

$query = "SELECT * FROM user_ach WHERE user_id = $cookie AND ach_id > 0 and ach_id 

 

NOTE - You will have to iterate through a while loop instead of the for loop.

 

Link to comment
https://forums.phpfreaks.com/topic/161605-order-by-in-a-loop/#findComment-853231
Share on other sites

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.