tom_b Posted February 17, 2007 Share Posted February 17, 2007 Well, let me try to explain my problem again. I'm doing two queries to a table, everything works fine, but I want to sort the output based on the "select sum" query. Here's the code: $query = 'SELECT ed, ted, tom FROM week ORDER BY id DESC LIMIT 1'; $result = mysql_query($query) or die(mysql_error()); $values['ed']['current'] = mysql_result($result, 0, "ed"); $values['ted']['current'] = mysql_result($result, 0, "ted"); $values['tom']['current'] = mysql_result($result, 0, "tom"); $query = 'SELECT SUM(ed) AS ed, SUM(ted) AS ted, SUM(tom) AS tom FROM week'; $result = mysql_query($query) or die(mysql_error()); $values['ed']['sum'] = mysql_result($result, 0, "ed"); $values['ted']['sum'] = mysql_result($result, 0, "ted"); $values['tom']['sum'] = mysql_result($result, 0, "tom"); $values['ed']['name'] = mysql_field_name($result, 0); $values['ted']['name'] = mysql_field_name($result, 1); $values['tom']['name'] = mysql_field_name($result, 2); foreach ($values as $key => $row) { $name[$key] = $row['name']; $current[$key] = $row['current']; $sum[$key] = $row['sum']; echo "<font color ='blue' font size = '3'>$name[$key]";? ></td><td width="35"><?php echo "<font color ='blue' font size = '3'>$current[$key]"; ?></td><td><?php echo "<font color ='blue' font size = '3'>$sum[$key] The output I get now looks something like this: Name Current Sum Ed 25 50 Ted 30 70 Tom 28 60 I need to sort the rows based on the values of the sum column. I've tried all sorts of "Order by" variations etc, just don't know where to go from here. Thanks, Tom Quote Link to comment https://forums.phpfreaks.com/topic/38854-sort-rows-from-two-queries/ Share on other sites More sharing options...
hitman6003 Posted February 17, 2007 Share Posted February 17, 2007 Please don't double post. Use array_multisort.... http://www.php.net/array_multisort Quote Link to comment https://forums.phpfreaks.com/topic/38854-sort-rows-from-two-queries/#findComment-186851 Share on other sites More sharing options...
ToonMariner Posted February 17, 2007 Share Posted February 17, 2007 swicth the keys on your array and then use multisort like so <?php $values['sum']['ed'] = mysql_result($result, 0, "ed"); $values['sum']['ted'] = mysql_result($result, 0, "ted"); $values['sum']['tom'] = mysql_result($result, 0, "tom"); $values['name']['ed'] = mysql_field_name($result, 0); $values['name']['ted'] = mysql_field_name($result, 1); $values['name']['tom'] = mysql_field_name($result, 2); array_multisort($values['sum'] , SORT_NUMERIC, SORT_DESC, $values['name'] , SORT_STRING, SORT_ASC; ?> Then use foreach($values['sum'] as $key => $val) ..... You can do the rest - I'm knackered and drunk(ish) Quote Link to comment https://forums.phpfreaks.com/topic/38854-sort-rows-from-two-queries/#findComment-186852 Share on other sites More sharing options...
sasa Posted February 17, 2007 Share Posted February 17, 2007 or try <?php function ms($a, $b) { if ($a['sum'] < $b['sum']) return 1; elseif ($a['sum'] > $b['sum']) return -1; else return 0; } $values = array('ed' => array('current' => 25, 'sum' => 50, 'name' => 'ed'), 'ted' => array('current' => 30, 'sum' => 70, 'name' => 'ted'), 'tom' => array('current' => 28, 'sum' => 60, 'name' => 'tom') ); usort($values, 'ms'); print_r($values); ?> Quote Link to comment https://forums.phpfreaks.com/topic/38854-sort-rows-from-two-queries/#findComment-186966 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.