Search code examples
phpmysqlsql-order-byauto-incrementautonumber

Order by Total For Sum function used


    <?
$tablae = mysql_query("SELECT * FROM order_history where (type!='rent_referral' AND type!='rental_balance') AND date>'" . strtotime($time1) . "' AND date<'" . strtotime($time2) . "'  GROUP BY user_id"); 
while ($order = mysql_fetch_array($tablae)) { 

?>
<tr>
<?
$tablaes = mysql_query("SELECT * FROM members where id='$order[user_id]'"); 
$user = mysql_fetch_array($tablaes);
$idsd=$user['id'];
$rPaid=mysql_query("SELECT SUM(`price`) AS total FROM order_history WHERE (type!='rent_referral' AND type!='rental_balance') AND date>'" . strtotime($time1) . "' AND date<'" . strtotime($time2) . "'");
$hdPaid = mysql_fetch_array($rPaid);
$sPaid=mysql_query("SELECT SUM(`price`) AS total FROM order_history WHERE user_id='$idsd' AND (type!='rent_referral' AND type!='rental_balance') AND date>'" . strtotime($time1) . "' AND date<'" . strtotime($time2) . "'");
while ($hPaid = mysql_fetch_array($sPaid)) { 
?>
<td><?=$user['username']?></td>
<td><?=$hPaid['total']?></td>
<?  
}
?>

</tr>

<? } ?>

This gets me this result http://dl.dropbox.com/u/14384295/test.jpeg

I want to order the price totals by DESC.

I would need

$sPaid=mysql_query("SELECT SUM(`price`) AS total FROM order_history WHERE user_id='$idsd' AND (type!='rent_referral' AND type!='rental_balance') AND date>'" . strtotime($time1) . "' AND date<'" . strtotime($time2) . "'");

the total on that to be ordered by DESC.


Solution

  • You could either use client side sorting with javascript, there are some nice jQuery addons that can do that.

    Or you have to totaly rewrite your code to have a single sql using joins and group by.

    But I cannot realy follow the logic with $rPaid, $hPaid and $sPaid so I cannot help you there.