astralaaron Posted November 7, 2009 Share Posted November 7, 2009 Hi, in my table I have a player name and score...Rodrigo - 2700Rick -2200Tammy - 1000Jennifer - 400The scores change constantly.I am just wondering: say each person has a profile... how would you display their rank? in Rodrigo's #1 Ricks #2 etc...it sounded like something simple in my mind at first but I started confusing it.right now I think I can make it work by looping through the table and incrementing a variable then breaking the loop once it reaches the right user..?anyway, I was just wondering how you guys would do it. Link to comment Share on other sites More sharing options...
jeffman Posted November 7, 2009 Share Posted November 7, 2009 Not quite sure I follow, but maybe this will help: http://www.w3schools.com/sql/sql_orderby.asp Link to comment Share on other sites More sharing options...
Redroest Posted November 7, 2009 Share Posted November 7, 2009 I think it is best to put them in an array first: <?php$scores["Rodrigo"] = 2700;$scores["Tammy "] = 1000;$scores["Rick "] = 2200;$scores["Jennifer "] = 400;sort($scores); //Build-in php function to sort array'secho "scores for today sorted by highst points";foreach ($scores AS $key => $val) { echo $key." with ".$val." points.<br />";}?> Link to comment Share on other sites More sharing options...
astralaaron Posted November 7, 2009 Author Share Posted November 7, 2009 i realize how I can list all of the users and ranks. I'm saying within their profile it will say "Rank #"but how do you get that number?EDIT: within the users personal profile how do I just get their rank? Link to comment Share on other sites More sharing options...
Redroest Posted November 7, 2009 Share Posted November 7, 2009 It is not clear to me what you have and what you want. Can you give me some code that you have right now? Link to comment Share on other sites More sharing options...
astralaaron Posted November 7, 2009 Author Share Posted November 7, 2009 It is not clear to me what you have and what you want. Can you give me some code that you have right now?hmm I don't see how I can make it any more clear.they each have a profile and I want to display their rank inside of their profile. there is no code yet just the table in the database. Link to comment Share on other sites More sharing options...
Redroest Posted November 7, 2009 Share Posted November 7, 2009 Ok:Step 1: Make links like:<a href='profile.php?name=Rodrigo'>Rodrigo</a><br /><a href='profile.php?name=Tammy'>Tammy</a><br /><a href='profile.php?name=Rick'>Rick</a><br /><a href='profile.php?name=Jennifer'>Jennifer</a><br />Step 2: Make a profile.phpStep 3: Make a query in profiles.php. My example is in MySQLi <?php//$_GET['name'] contains the name given in the url//Never forget to secure $_GET globals to prevent SQL-injection$name = mysqli_real_escape_string($mysqli, $_GET['name']);//Make query$sql = "SELECT * FROM profiles WHERE name='".$name."'";//Activate query and give error on failureif(!$Profiles= $mysqli->query($sql)){ trigger_error('Fout in query: '.$mysqli->error);}//Query succeededelse{ //Get profile data if($profile= $Profiles->fetch_assoc()) { //Show the score of the given profile echo "<p><strong>Welcome on the profile of ".$profile['name']."</strong></p>"; echo $profile['name']" now has ".$profile['score']." points!!<br />"; }}?> Link to comment Share on other sites More sharing options...
astralaaron Posted November 7, 2009 Author Share Posted November 7, 2009 Ok:Step 1: Make links like:<a href='profile.php?name=Rodrigo'>Rodrigo</a><br /><a href='profile.php?name=Tammy'>Tammy</a><br /><a href='profile.php?name=Rick'>Rick</a><br /><a href='profile.php?name=Jennifer'>Jennifer</a><br />Step 2: Make a profile.phpStep 3: Make a query in profiles.php. My example is in MySQLi echo $profile['name']" now has ".$profile['score']." points!!<br />"; I want this: echo $profile['name']" is Rank # ".$rank." !!<br />"; do you understand? Link to comment Share on other sites More sharing options...
Synook Posted November 7, 2009 Share Posted November 7, 2009 http://arjen-lentz.livejournal.com/56292.htmlSELECT COUNT(*)+1 FROM profiles WHERE score > (SELECT score FROM profiles WHERE name='$name') Basically "tell me how many people, plus one, there are with a score higher than that of {$name}s". Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.