tom_b Posted February 20, 2007 Share Posted February 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/39250-sort-mysql-output/ Share on other sites More sharing options...
btherl Posted February 20, 2007 Share Posted February 20, 2007 Can you show us the query you have now? Quote Link to comment https://forums.phpfreaks.com/topic/39250-sort-mysql-output/#findComment-189222 Share on other sites More sharing options...
fenway Posted February 20, 2007 Share Posted February 20, 2007 My guess is a missing column alias. Quote Link to comment https://forums.phpfreaks.com/topic/39250-sort-mysql-output/#findComment-189442 Share on other sites More sharing options...
tom_b Posted February 20, 2007 Author Share Posted February 20, 2007 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??? Quote Link to comment https://forums.phpfreaks.com/topic/39250-sort-mysql-output/#findComment-189456 Share on other sites More sharing options...
fenway Posted February 20, 2007 Share Posted February 20, 2007 Well, first, that's not the most efficient way to get data out a result set... second, I don't see any ORDER BY clause... and third, you have column alias "AS ted". Quote Link to comment https://forums.phpfreaks.com/topic/39250-sort-mysql-output/#findComment-189560 Share on other sites More sharing options...
tom_b Posted February 21, 2007 Author Share Posted February 21, 2007 I'm sorry, I'm still very new to this, just trying to learn as I go and get a little help when I can. Quote Link to comment https://forums.phpfreaks.com/topic/39250-sort-mysql-output/#findComment-190149 Share on other sites More sharing options...
fenway Posted February 21, 2007 Share Posted February 21, 2007 Does that mean you need me to clarify? Quote Link to comment https://forums.phpfreaks.com/topic/39250-sort-mysql-output/#findComment-190221 Share on other sites More sharing options...
tom_b Posted February 21, 2007 Author Share Posted February 21, 2007 I would appreciate any help you could give me, even just some hints to point me in the right direction. Thanks, Tom Quote Link to comment https://forums.phpfreaks.com/topic/39250-sort-mysql-output/#findComment-190397 Share on other sites More sharing options...
fenway Posted February 21, 2007 Share Posted February 21, 2007 Well, if you want to order your result by one of the expressions, simply add "ORDER BY ted ASC" to the end of your query. Quote Link to comment https://forums.phpfreaks.com/topic/39250-sort-mysql-output/#findComment-190543 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.