Bluehost.com Web Hosting $6.95

Code generated sort

This is a discussion on Code generated sort within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I wish to display a series of rows in a table, ordered by the column 'order total'. 'order total' is ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2006
David Ehmer
 
Posts: n/a
Default Code generated sort

I wish to display a series of rows in a table, ordered by the column 'order
total'. 'order total' is calculated in PHP rather than being available as a
database field, therefore I can't use the 'order by' option in my query.

The code I'm using to display the table rows is:

$userPoints = array();
while ($row = mysql_fetch_assoc($result)) {
$userPoints[$row["username"]][$row["round"]] = $row["points"];
}
foreach($userPoints as $username => $rounds) {
echo "<tr>";
echo "<td>".$username."</td>";
$total = 0;
for($i = 1; $i < 27; $i++) {
echo "<td>".($rounds[$i] ? $rounds[$i] : "-")."</td>";
$total += $rounds[$i];
}
echo "<td><strong>".$total."</strong></td>";
echo "</tr>";
}

Is it possible to order (highest to lowest) the rows using a PHP sort
function like arsort?

If so how does that function fit into the code structure above and what is
the correct syntax. I'm having trouble finding useful examples of this
beyond the very basic intro in the PHP manual.

Thanks
David


Reply With Quote
  #2 (permalink)  
Old 03-01-2006
Michael Fesser
 
Posts: n/a
Default Re: Code generated sort

..oO(David Ehmer)

>I wish to display a series of rows in a table, ordered by the column 'order
>total'. 'order total' is calculated in PHP rather than being available as a
>database field, therefore I can't use the 'order by' option in my query.


You can let the database calculate it:

SELECT username, SUM(points) AS total
FROM yourTable
GROUP BY username
ORDER BY total DESC

Or something like that. Then you could use the result from this query to
display the user records in the order from highest to lowest points.

Micha
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:38 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0