Jump to content

[SOLVED] ORDER BY $variable DESC


ArizonaJohn

Recommended Posts

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

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;

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.