Skip to main content
May 15, 2009
Answered

PHP - Sorting a Table in Descending Order

  • May 15, 2009
  • 1 reply
  • 679 views

Hello,

I am creating the HTML table below in PHP.  I would like to sort it by the value $effective vote in descending order.  How do I do that?

Thanks,

John

if(mysql_num_rows($result)>0){
while($table=mysql_fetch_row($result)){
print "<p class=\"topic\">$table[0]</p>\n";
$r=mysql_query("SELECT * FROM `$table[0]`");


print "<table class=\"navbar\">\n";
while($row=mysql_fetch_array($r)){

$effective_vote = $row['votes_up'] - $row['votes_down'];

print "<tr>";

print "<td>".'<a href="http://'.$row['site'].'" class="links2">'.$row['site'].'</a>'."</td>";
print "<td class='votes'>".'<span class="votes_count" id="votes_count'.$row['id'].'">'.number_format($effective_vote).'</span>'."</td>";
print "<td class='ballot'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.Vote.'</a>'.'</span>'."</td>";
}
print "</tr>\n";
}
print "</table>\n";

This topic has been closed for replies.
Correct answer David_Powers

SELECT  * , votes_up - votes_down AS effective_vote
FROM  `$table[0]`
ORDER  BY effective_vote DESC

This also gives you $row['effective_vote'] as part of your database result.

1 reply

David_Powers
David_PowersCorrect answer
Inspiring
May 15, 2009

SELECT  * , votes_up - votes_down AS effective_vote
FROM  `$table[0]`
ORDER  BY effective_vote DESC

This also gives you $row['effective_vote'] as part of your database result.

May 15, 2009

Thanks David,

It works like a charm!

I appreciate the help.

Regards,

John