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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
..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 |