Jump to content

sort rows from two queries


tom_b

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/38854-sort-rows-from-two-queries/
Share on other sites

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)

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);
?>

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.