srhino Posted January 25, 2008 Share Posted January 25, 2008 Hey everyone! I am trying to build a really simple golf leaderboard. Is there a way to build it so when they data records show it will automatically sort the records in order? I would like it to sort similar to below 1 John -4 2 Scott -3 2 Fred -3 2 xxxx -3 5 xxxx -2 5 xxxx -2 7 xxxx -1 8 xxxx 0 Thanks in advance for your input Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2008 Share Posted January 25, 2008 Where are you getting the data from? Is it from a file? An array? A SQL query? Let us know and show us some code! Quote Link to comment Share on other sites More sharing options...
srhino Posted January 25, 2008 Author Share Posted January 25, 2008 I will query the following from a database. team player1 player2 player3 player4 player5 score thru Here is the code that I have started. I have not finished writing yet because I wasn't sure where to go. <?php $con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase'); $query = "SELECT * FROM scoring order by rank" ; $result = mysql_query($query); echo " <table width=\"100%\" border=\"1\" align=\"center\"> <tr> <br> <td>Rank</td> <td>Team Name</td> <td>Player 1</td> <td>Player2</td> <td>Player3</td> <td>Player4</td> <td>Player5</td> <td>Score</td> <td>Thru</td></TR> <tr> <td>$rank</td> <td>$team</td> <td>$p1</td> <td>$p2</td> <td>$p3</td> <td>$p4</td> <td>$p5</td> <td>$score</td> <td>$th</td> </tr> </table>"; ?> Quote Link to comment Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2008 Share Posted January 25, 2008 Ok you just need to include the loop in there to retrieve the info and it should be fine. <?php $con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase'); $query = "SELECT * FROM scoring order by rank" ; $result = mysql_query($query) or die(mysql_error()); echo " <table width=\"100%\" border=\"1\" align=\"center\"> <tr> <td>Rank</td> <td>Team Name</td> <td>Player 1</td> <td>Player2</td> <td>Player3</td> <td>Player4</td> <td>Player5</td> <td>Score</td> <td>Thru</td></TR>"; while($row = mysql_fetch_array($result)) { echo "<tr> <td>".$row['rank']."</td> <td>".$row['team']."</td> <td>".$row['p1']."</td> <td>".$row['p2']."</td> <td>".$row['p3']."</td> <td>".$row['p4']."</td> <td>".$row['p5']."</td> <td>".$row['score']."</td> <td>".$row['thru']."</td> </tr> } echo "</table>"; ?> Hopefully that should do what you want, make sure you edit the formatting of the HTML to how you want to though, and sorry if there's any errors in there (I'm a bit tired). Quote Link to comment Share on other sites More sharing options...
srhino Posted January 25, 2008 Author Share Posted January 25, 2008 Sorry, I might not have explained myself that well. I know how to make it repeat all the information and output it to my sheet. How can it rank the scores in order automatically without me having to put the rank in? Thanks, Quote Link to comment Share on other sites More sharing options...
resago Posted January 25, 2008 Share Posted January 25, 2008 so you want by rank, then by score asc. Quote Link to comment Share on other sites More sharing options...
srhino Posted January 25, 2008 Author Share Posted January 25, 2008 Actually just by score is fine...I actually want it to assign a rank or place by the scores. Quote Link to comment Share on other sites More sharing options...
resago Posted January 26, 2008 Share Posted January 26, 2008 ok, query and sort by score ascending when displaying, start with 1 and as the score changes, add to rank. ex. $rank=0; $r=""; while($row = mysql_fetch_array($result)) { if ($r!=$row['score']){$rank++} $r=$row['score']; echo "<tr> <td>".$rank."</td> <td>".$row['team']."</td> <td>".$row['p1']."</td> <td>".$row['p2']."</td> <td>".$row['p3']."</td> <td>".$row['p4']."</td> <td>".$row['p5']."</td> <td>".$row['score']."</td> <td>".$row['thru']."</td> </tr>"; } this should be close to what you want. Quote Link to comment Share on other sites More sharing options...
srhino Posted January 26, 2008 Author Share Posted January 26, 2008 I am getting there. One more thing. where would I put that code... Here is how I wrote it so far... <?php $con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase'); $query = "SELECT * FROM scoring order by score" ; $result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved'); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $rank = $row['rank']; $team = $row['team']; $p1 = $row['p1']; $p2 = $row['p2']; $p3 = $row['p3']; $p4 = $row['p4']; $p5 = $row['p5']; $score = $row['score']; $th = $row['th']; echo "$rank, $team, $score, $th";} ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted January 26, 2008 Share Posted January 26, 2008 Look at his while loop and look at your while loop, copy/paste appropriately. Quote Link to comment Share on other sites More sharing options...
srhino Posted January 26, 2008 Author Share Posted January 26, 2008 I have tried that but it doesn't work. I'm not very good at this I guess. <?php $con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase'); $query = "SELECT * FROM scoring order by score" ; $result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved'); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $rank = $row['rank']; $team = $row['team']; $p1 = $row['p1']; $p2 = $row['p2']; $p3 = $row['p3']; $p4 = $row['p4']; $p5 = $row['p5']; $score = $row['score']; $th = $row['th']; // where does the following go? $rank=0; $r=""; // with this while statement does it go at the top where I have the other while statement? while($row = mysql_fetch_array($result)) { if ($r!=$row['score']){$rank++} $r=$row['score']; echo "$rank, $team, $score, $th<br>";} ?> Quote Link to comment Share on other sites More sharing options...
resago Posted January 26, 2008 Share Posted January 26, 2008 <?php $con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase'); $query = "SELECT * FROM scoring order by score" ; $result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error()); #$row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved'); $rank=0; $r=""; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { #$rank = $row['rank']; $team = $row['team']; $p1 = $row['p1']; $p2 = $row['p2']; $p3 = $row['p3']; $p4 = $row['p4']; $p5 = $row['p5']; $score = $row['score']; $th = $row['th']; if ($r!=$score){$rank++} $r=$score; echo "$rank, $team, $score, $th<br>";} ?> Quote Link to comment Share on other sites More sharing options...
srhino Posted January 26, 2008 Author Share Posted January 26, 2008 OK, thats what I did the first time and it didn't work and it still doesn't work. Where else could I be going wrong? When I run the program nothing shows not even an error message. <?php $con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase'); $query = "SELECT * FROM scoring order by score" ; $result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error()); #$row = mysql_fetch_array($result, MYSQL_ASSOC) or die('No records retrieved'); $rank=0; $r=""; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { #$rank = $row['rank']; $team = $row['team']; $p1 = $row['p1']; $p2 = $row['p2']; $p3 = $row['p3']; $p4 = $row['p4']; $p5 = $row['p5']; $score = $row['score']; $th = $row['th']; if ($r!=$score){$rank++} $r=$score; echo "$rank, $team, $score, $th<br>";} ?> Quote Link to comment Share on other sites More sharing options...
marcus Posted January 26, 2008 Share Posted January 26, 2008 lul. try <?php $con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase'); $query = "SELECT * FROM scoring order by score"; $result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error ()); $rank = 1; echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<tr><td>Rank</td><td>Team</td><td>Score</td><td>Th</td></tr>\n"; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { #$rank = $row['rank']; $team = $row['team']; for($i=1;$i<=5;$i++){ $p[$i] = $row['p' . $i]; } $score = $row['score']; $th = $row['th']; echo "<tr><td>".$rank."</td><td>".$team."</td><td>".number_format($score)."</td><td>".$th."</td></tr>\n"; $rank++; } echo "</table>\n"; ?> Quote Link to comment Share on other sites More sharing options...
resago Posted January 26, 2008 Share Posted January 26, 2008 is there info in the DB, is score one of the rows? Quote Link to comment Share on other sites More sharing options...
resago Posted January 26, 2008 Share Posted January 26, 2008 lul. try <?php $con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase'); $query = "SELECT * FROM scoring order by score"; $result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error ()); $rank = 1; echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<tr><td>Rank</td><td>Team</td><td>Score</td><td>Th</td></tr>\n"; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { #$rank = $row['rank']; $team = $row['team']; for($i=1;$i<=5;$i++){ $p[$i] = $row['p' . $i]; } $score = $row['score']; $th = $row['th']; echo "<tr><td>".$rank."</td><td>".$team."</td><td>".number_format($score)."</td><td>".$th."</td></tr>\n"; $rank++; } echo "</table>\n"; ?> not quite. his rank changes as score changes, not by player returned. Quote Link to comment Share on other sites More sharing options...
marcus Posted January 26, 2008 Share Posted January 26, 2008 i didn't bother reading the topic. Quote Link to comment Share on other sites More sharing options...
resago Posted January 26, 2008 Share Posted January 26, 2008 Quote Link to comment Share on other sites More sharing options...
srhino Posted January 26, 2008 Author Share Posted January 26, 2008 First of all thank all of you for your help. I hope I am not being a pain. I am just trying to learn this stuff and my teachers are not that good at explaining this. I am learning more from you guys than I am in School!! Yeah score is one of the rows. That time it worked. It output the following...but Is there a way for it to show ties? Rank Team Score Th 1 Modells -3 9 2 GolfSmith -3 8 3 Happy China -2 5 4 Ground Round -1 5 so it would read like this Rank 1 Modells 1 Golfsmith 3 4 Quote Link to comment Share on other sites More sharing options...
marcus Posted January 26, 2008 Share Posted January 26, 2008 Suree. $score_array = array(); $score_array[] = $row['scores']; if(!in_array($row['scores'],$score_array)){ $rank++; } Quote Link to comment Share on other sites More sharing options...
srhino Posted January 26, 2008 Author Share Posted January 26, 2008 Where do I put that? Quote Link to comment Share on other sites More sharing options...
marcus Posted January 26, 2008 Share Posted January 26, 2008 while statement. Quote Link to comment Share on other sites More sharing options...
srhino Posted January 26, 2008 Author Share Posted January 26, 2008 like this? I put it there but it didn't work. <?php $con = mysql_connect("localhost", "test", "test") or die('Could not connect to database'); mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase'); $query = "SELECT * FROM scoring order by score"; $result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error ()); $rank = 1; echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<tr><td>Rank</td><td>Team</td><td>Score</td><td>Th</td></tr>\n"; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { #$rank = $row['rank']; $score_array = array(); $score_array[] = $row['scores']; if(!in_array($row['scores'],$score_array)){ $rank++; } $team = $row['team']; for($i=1;$i<=5;$i++){ $p[$i] = $row['p' . $i]; } $score = $row['score']; $th = $row['th']; echo "<tr><td>".$rank."</td><td>".$team."</td><td>".number_format($score)."</td><td>".$th."</td></tr>\n"; $rank++; } echo "</table>\n"; ?> Quote Link to comment Share on other sites More sharing options...
srhino Posted January 26, 2008 Author Share Posted January 26, 2008 Please we are so close...can any help me with the placement of the array in the while statement? Quote Link to comment Share on other sites More sharing options...
srhino Posted January 26, 2008 Author Share Posted January 26, 2008 OK I would like to thank all of you for taking your time helping. I figured it out!!! In case anyone ever needs to know. Here is how I did it... <?php $con = mysql_connect("XXXXX", "XXXXXXX", "XXXXXX") or die('Could not connect to database'); mysql_select_db("bestball", $con) or die('Sory could not connect to the dadtabase'); $query = "SELECT * FROM scoring order by score"; $result = mysql_query($query) or die('Could not find Tournament: ' . mysql_error ()); $rank = 1; $recordCounter=1; $previousScore=0; echo "<table border=\"1\" cellspacing=\"3\" cellpadding=\"3\" align=\"center\">\n"; echo "<tr> <td>Rank</td> <td>Team</td> <td>Score</td> <td>Thru</td> <td>Player 1</td> <td>Player 2</td> <td>Player 3</td> <td>Player 4</td> <td>Celebrity</td> </tr>\n"; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $score_array = array(); $score_array[] = $row['scores']; //comment out the following line. //if(!in_array($row['scores'],$score_array)){ $rank++; } $team = $row['team']; for($i=1;$i<=5;$i++){ $p[$i] = $row['p' . $i]; } $score = $row['score']; $th = $row['th']; $p1 = $row['p1']; $p2 = $row['p2']; $p3 = $row['p3']; $p4 = $row['p4']; $p5 = $row['p5']; if ($previousScore != $score){$rank = $recordCounter;} echo " <tr> <td>".$rank."</td> <td>".$team."</td> <td><h9 align=\"right\">".number_format($score)."</h9></td> <td>".$th."</td> <td>".$p1."</td> <td>".$p2."</td> <td>".$p3."</td> <td>".$p4."</td> <td>".$p5."</td> </tr>\n"; $recordCounter++; $previousScore = $score; } echo "</table>\n"; ?> Quote Link to comment 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.