BelowZero Posted April 7, 2011 Share Posted April 7, 2011 The following code works, but I need to take it a step further and I'm not sure how. What I'd like to do is sort the output so the highest point totals get printed first (along with the associated information). Can anyone help? Here's the site: http://www.beat-the-spread.net/seasontotals.php Here's the code: $i=1; $w=1; $result = mysql_query( " SELECT team_name, owner FROM teams" ); while ($row = mysql_fetch_array($result)) { echo "<tr>\n", "</td><td>", $row['owner'], "</td><td>", $row['team_name']; $result1 = mysql_query( " SELECT bye, owner_pts FROM schedule WHERE team = $i"); $num_rows = mysql_num_rows($result1); while ($row1 = mysql_fetch_array($result1)) { $totalpts = $totalpts + $row1['owner_pts']; if (empty($row1['bye'])) { echo "</td><td>", $row1['owner_pts'], "</tr>\n"; } else { echo "</td><td>","0","</tr>\n"; } } while ($num_rows<=16) { echo "</td><td>","","</tr>"; $num_rows++; } echo "</td><td>","$totalpts","</tr>"; $totalpts=0; $w++; $i++; } echo "</table>\n"; mysql_close($con); Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/233030-sorting-an-array/ Share on other sites More sharing options...
gex80 Posted April 7, 2011 Share Posted April 7, 2011 I would assume that in your mysql query you use the sort function with a where clause. then it would save it to the array in the order that it is sorted in. What I would do is first echo out the sql results to see if it is in the order you want and then after that pass it into an array. Quote Link to comment https://forums.phpfreaks.com/topic/233030-sorting-an-array/#findComment-1198481 Share on other sites More sharing options...
dcro2 Posted April 7, 2011 Share Posted April 7, 2011 This should do it: $result1 = mysql_query( " SELECT bye, owner_pts FROM schedule WHERE team = $i ORDER BY owner_pts DESC"); Quote Link to comment https://forums.phpfreaks.com/topic/233030-sorting-an-array/#findComment-1198486 Share on other sites More sharing options...
BelowZero Posted April 7, 2011 Author Share Posted April 7, 2011 I'm not storing a total in the database, only the points per week. So, I have to figure the totals on the fly. It's while I'm doing the totaling that I want to sort the output. Might be easier to create another field and store the totals in the database. Quote Link to comment https://forums.phpfreaks.com/topic/233030-sorting-an-array/#findComment-1198489 Share on other sites More sharing options...
dawsba Posted April 8, 2011 Share Posted April 8, 2011 rough and not tested, there is a better way of sorting a multiD array, but im too tired lol also i tidied up your html <? $i=1; $w=1; $data = array(); $result = mysql_query("SELECT team_name, owner FROM teams "); while ($row = mysql_fetch_array($result)) { $result1 = mysql_query("SELECT SUM(`owner_pts`) as total FROM `schedule` WHERE `team` = '$i' ORDER BY total ASC"); while ($row1 = mysql_fetch_array($result1)) { $data[][$row1['total']]['teamname']=$row['owner']; } } foreach($data as $k=>$v) { foreach($v as $k2=>$v2) { $rowd[$k]['owner']; $rowd[$k]['team_name']; } } ?> <table> <? foreach($rowd as $k=>$row) { ?> <tr> <td> <?= $row['owner']; ?> </td> <td> <?= $row['team_name']; ?> </td> </tr> <tr> <td colspan="2"> <table> <tr> <? $result1 = mysql_query("SELECT bye, owner_pts FROM schedule WHERE team = $i"); $num_rows = mysql_num_rows($result1); while ($row1 = mysql_fetch_array($result1)) { ?> <td> <? $totalpts = $totalpts + $row1['owner_pts']; if (empty($row1['bye'])) { echo $row1['owner_pts']; } else { echo "0"; } ?> </td> <? } while ($num_rows<=16) { echo "<td> </td>"; $num_rows++; } ?> <td><?= $totalpts; ?></td> <? $totalpts=0; $w++; $i++; ?> </table> </td> </tr> <? } ?> </table> <? mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/233030-sorting-an-array/#findComment-1198492 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.