Author Topic: [SOLVED] How do i sort a mysql_fetch_array  (Read 2149 times)

0 Members and 1 Guest are viewing this topic.

Offline WildhalfTopic starter

  • Enthusiast
  • Posts: 99
    • View Profile
[SOLVED] How do i sort a mysql_fetch_array
« on: February 01, 2007, 01:49:30 PM »
Hi 

I need some help. The While loop below returns and displays exactly what i want but i want to sort the rows by $row['no_referrals'] showing the largest first. Has anyone any ideas how i do this???

Code: [Select]
$sql = mysql_query("SELECT * FROM tblmembers WHERE referred_by='$member_id'") or
die(mysql_error());
 
//$num_rows = mysql_num_rows($sql_num_rows);

//now you could do something like this to make sure that you don't loop
//through the results if there aren't any to be looped through
if (mysql_num_rows($sql) < 1) {
    die("There are not matching results");

 
//now we loop through the results..
//for ( $counter = 0; $counter <= $num_rows ; $counter += 1) {
while($row = mysql_fetch_array($sql)) {

echo '<tr bgcolor="#FFFFCC">
<td  align="center" width="160" height="20" bgcolor="#eeeeee">
<div align="center" class="style5">';
echo $row['firstname'];
echo ' ' ;
echo $row['lastname'];
echo '<div>
</td>
<td width="160" height="20" bgcolor="#eeeeee">
<div align="center" class="style5"> ';
echo $row['no_referrals'];
echo '</div>
</td>
<td width="160" height="20" bgcolor="#FFFFCC" scope="row">
<div align="center" class="style5"> ';
echo '<a href="mailto:';
echo $row['email'];
echo '">';
echo $row['email'];
echo '</a>';
echo '</div>
</td>
  </tr> ';

}


Offline Orio

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,496
  • Gender: Male
    • View Profile
    • OriosRiddle
Re: How do i sort a mysql_fetch_array
« Reply #1 on: February 01, 2007, 01:51:49 PM »
Code: [Select]
$sql = mysql_query("SELECT * FROM tblmembers WHERE referred_by='$member_id' ORDER BY no_referrals DESC") or
die(mysql_error());

Orio.
Think you're smarty?

(Gone until 20 to November)