Jump to content

ehmer

New Members
  • Posts

    8
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

ehmer's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thanks wickning1 That fixed it up. Appreciate your help. David
  2. Hi I have almost completed an online football tipping competition. I have a list of records retrieved from a database that are displayed in a table showing points for each round for each user and a running season total. Some calculations are made on the data in the query and in the returned record set in the PHP code. A paragraph is displayed just before the table to indicate to the current user what position they hold. So if a user has some successful selections they will receive a message like this: "--current user--, you are currently in position 10 out of 250 players on the tipping ladder." If the user has no correct selections to date they will receive this message: "--current user--, you currently don't have a ranking as you haven't scored any points in the tipping competition yet." The table displays fine and the appropriate messages are returned with one exception. The user sitting in the top position in the table receives the 2nd message above rather than " ... you are currently in position 1 out of ..." Not sure what is going on here. The relevant code is detailed below. Appreciate any thoughts about what is going wrong here and how to overcome it. thanks, David [b]QUERY:[/b] SELECT tips.username, fixtures.round, SUM(CASE WHEN tips.tip='draw' AND fixtures.result='draw' then 1 WHEN tips.tip=fixtures.result then 1 ELSE 0 end) points FROM fixtures JOIN tips on (fixtures.round=tips.round AND fixtures.game=tips.game) GROUP BY tips.username, fixtures.round LIMIT 0,50 [b]CODE[/b] $userPoints = array(); while ($row = mysql_fetch_assoc($result)) { $userPoints[$row['username']][$row['round']] = $row['points']; $userPoints[$row['username']]['total'] += $row['points']; } function mysort ($a, $b) { return ($a['total'] == $b['total'] ? 0 : ($a['total'] > $b['total'] ? -1 : 1)); } uasort ($userPoints, "mysort"); // determine position of current user in relation to all players list($pos) = array_keys(array_keys($userPoints), $valid_user); if (!$pos) { echo "<p><strong>'$valid_user'</strong>, you currently don't have a ranking as you haven't scored any points in the tipping competition yet."; } else { $pos = $pos + 1; $number_of_players = count($userPoints); echo "<p><strong>'$valid_user'</strong>, you are currently in position <strong>".$pos."</strong> out of <strong>".$number_of_players."</strong> players on the tipping ladder.</p>"; } foreach($userPoints as $username => $rounds) { echo "<tr>"; echo "<td>".$username."</td>"; for($i = 1; $i < 27; $i++) { echo "<td>".($rounds[$i] ? $rounds[$i] : "-")."</td>"; } echo "<td><strong>".$rounds['total']."</strong></td>"; echo "</tr>"; } echo "</table>";
  3. Thanks uasort did the job. David
  4. It seems to be the usort function which is giving the problems with accessing the username. Not sure what specifically it is yet though. function mysort ($a, $b) { return ($a['total'] == $b['total'] ? 0 : ($a['total'] > $b['total'] ? -1 : 1)); } usort ($userPoints, "mysort");
  5. That's looking quite promising now. The sort order is working fine. I'm still getting the index number for the username column. (ie 0-7 rather than the names)
  6. This is the current query, but the database does not include a total for each user. That is calculated in the code. So I thought the query would not be useful for sorted by total. SELECT tips.username, fixtures.round, SUM(CASE WHEN tips.tip='draw' AND fixtures.result='draw' then 1 WHEN tips.tip=fixtures.result then 1 else 0 end) points FROM fixtures JOIN tips on (fixtures.round=tips.round AND fixtures.game=tips.game) GROUP BY tips.username, fixtures.round LIMIT 0,50
  7. I've tried unsuccessfully to incorporate your code into what I have now. Is it correct to put your 'foreach' and usort codeblocks before my existing 'foreach' block? Not sure how my foreach block needs to be modified. I've got varied results experimenting with this including sorting by username index (ie 0-7) and displaying integers in place of the username. Would appreciate any further help. Thanks David
  8. 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
×
×
  • 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.