Jump to content

Rank Based On Score?


astralaaron
 Share

Recommended Posts

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

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

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

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 />";  }}?>

Edited by Redroest
Link to comment
Share on other sites

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?

Edited by astralaaron
Link to comment
Share on other sites

http://arjen-lentz.livejournal.com/56292.html
SELECT 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...