ArizonaJohn Posted June 17, 2009 Share Posted June 17, 2009 Hello, I would like the code below to sort the echoed output by votes_up descending. I have added "ORDER BY votes_up DESC" to both queries, but the echoed output is still not sorting. Any ideas why it is not working? Thanks in advance, John $result = mysql_query("SHOW TABLES FROM feather") or die(mysql_error()); while(list($table)= mysql_fetch_row($result)) { $sqlA = "SELECT COUNT(*) FROM `$table` WHERE `site` LIKE '$entry' ORDER BY votes_up DESC"; $resA = mysql_query($sqlA) or die("$sqlA:".mysql_error()); list($isThere) = mysql_fetch_row($resA); if ($isThere) { $table_list[] = $table; } } foreach ($table_list as $table) { $sql = "SELECT votes_up FROM `$table` WHERE `site` LIKE '$entry' ORDER BY votes_up DESC"; $sql1 = mysql_query($sql) or die("$sql:".mysql_error()); while ($row = mysql_fetch_assoc($sql1)) { echo $table . ': "' . $row['votes_up'] . " for $entry from $table\"<br />"; } } Link to comment https://forums.phpfreaks.com/topic/162508-solved-order-by-variable-desc/ Share on other sites More sharing options...
ArizonaJohn Posted June 17, 2009 Author Share Posted June 17, 2009 Here's a solution that solved my problem: $result = mysql_query("SHOW TABLES FROM feather") or die(mysql_error()); while(list($table)= mysql_fetch_row($result)) { $sqlA = "SELECT COUNT(*) FROM `$table` WHERE `site` LIKE '$entry'"; $resA = mysql_query($sqlA) or die("$sqlA:".mysql_error()); list($isThere) = mysql_fetch_row($resA); if ($isThere) { $table_list[] = $table; } } foreach ($table_list as $table) { $sql = "SELECT votes_up FROM `$table` WHERE `site` LIKE '$entry'"; $sql1 = mysql_query($sql) or die("$sql:".mysql_error()); while ($row = mysql_fetch_assoc($sql1)) { $votes[$table] = $row['votes_up']; $sum += $row['votes_up']; } } arsort($votes); //reverse sort preserving keys foreach ($votes as $table => $votes) { echo $table . ': ' . $votes . ' for ' . $entry . ' from ' . $table . '<br />'; } echo $sum; Link to comment https://forums.phpfreaks.com/topic/162508-solved-order-by-variable-desc/#findComment-857779 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.