Jump to content

sort mysql output


tom_b

Recommended Posts

Hi!

 

I have a table which looks like this:

 

Name1        Name2        Name 3    ID

data            data            data        1

data            data            data        2

data            data            data        3

 

I can output the names, the last entry in each row and the totals from each row with no problem, but I can't sort the rows based on those totals, I'd appreciate any help or ideas!!!

 

Thanks, Tom

 

Link to comment
https://forums.phpfreaks.com/topic/39250-sort-mysql-output/
Share on other sites

Well, I've tried a few different things, this is what I started with.  This first query gives me the last entry into the database:

 

$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");

 

these next ones give me the totals and the row names:

 

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

 

this is what I use to get the output

 

foreach ($values as $key => $row) {

  $name[$key] = $row['name'];

  $current[$key]  = $row['current'];

  $sum[$key] = $row['sum'];

 

this all works fine, I just can't sort the rows based on the sums.  Thanks for taking the time to look at it!!

 

Tom

 

P.S.  What is a missing column alias???

Link to comment
https://forums.phpfreaks.com/topic/39250-sort-mysql-output/#findComment-189456
Share on other sites

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.