Jump to content

ORDER BY and while loops


kingb00zer

Recommended Posts

Hi I am currently working on a part of my site which displays all of the users in a rankings page. What I want to achieve is a page that lists the users from largest to lowest based on a number in each players row in a table in my database, that number being "networth". So I have done a lot of work with while loops running thrugh all of the users until I get a count variable to match the total number of users whilst matching the count variable to the players id number in the database. But what im having trouble with is getting the order displayed from highest networth to lowest networth, I cant seem to find how to match each repitition of the loop to individual users based on what their "networth" is. When using ORDER BY Does it give everything in that query a number based on its order in the list? If so what do I write to call it on so I can macth it to a count variable and display everyone in order instead of just one person several times over as I get with the code below.

echo "<table class='mainpage' align='center'>";echo "<tr><td class='stats'>Rank</td><td class='stats'>Player</td><td class='stats'>Thugs</td><td class='stats'>Dealers</td><td class='stats'>Drugs</td><td class='stats'>Networth</td>  </tr>";$rank2=mysql_query("SELECT username, dealers, thugs, drugs, networth FROM playerstats "); //get first player while ($rank <= $rowcount){	$rank1=mysql_query("SELECT username, dealers, thugs, drugs, networth FROM playerstats ORDER BY networth ASC   ");		while($row=mysql_fetch_array($rank1))	{	$username= $row['username'];	$dealers= $row['dealers'];	$thugs= $row['thugs'];	$drugs= $row['drugs'];	$drugs= number_format($drugs);	$networth= $row['networth'];	$networth= number_format($networth);	}	echo "<tr><td class='stats'>$rank</td><td class='stats'>$username</td><td class='stats'>$dealers</td><td class='stats'>$thugs</td><td class='stats'>$drugs</td><td class='stats'>$networth</td> </tr>";	$rank++;} echo "</table>";

Link to comment
Share on other sites

my bad I was in a hurry and only cut out the last part of the code forgetting that $rank and $rowcount was declared earlier, full code below.

<?phpsession_start();include('connection.php');// get player total count$query= "SELECT * FROM playerstats";$qry= mysql_query($query);$rowcount= mysql_num_rows($qry);$rank= 1;echo "<html>";echo "<head><link rel='stylesheet' type='text/css' href='gamestyles.css' /> </head>";echo "<title> Game.</title>";echo "<body>";echo "<table class='mainpage' align='center'>";echo "<tr><td class='stats'>Rank</td><td class='stats'>Player</td><td class='stats'>Thugs</td><td class='stats'>Dealers</td><td class='stats'>Drugs</td><td class='stats'>Networth</td>  </tr>";$rank2=mysql_query("SELECT username, dealers, thugs, drugs, networth FROM playerstats "); //get first player while ($rank <= $rowcount){	$rank1=mysql_query("SELECT username, dealers, thugs, drugs, networth FROM playerstats ORDER BY networth ASC   ");		while($row=mysql_fetch_array($rank1))	{	$username= $row['username'];	$dealers= $row['dealers'];	$thugs= $row['thugs'];	$drugs= $row['drugs'];	$drugs= number_format($drugs);	$networth= $row['networth'];	$networth= number_format($networth);	}	echo "<tr><td class='stats'>$rank</td><td class='stats'>$username</td><td class='stats'>$dealers</td><td class='stats'>$thugs</td><td class='stats'>$drugs</td><td class='stats'>$networth</td> </tr>";	$rank++;} echo "</table>";?>

Link to comment
Share on other sites

Your current system is performing too many queries and not extracting the right data.All you need is an independent variable that increases on each iteration of the loop. Remove all your queries and just use this:

echo "<table class='mainpage' align='center'>";echo "<tr><td class='stats'>Rank</td><td class='stats'>Player</td><td class='stats'>Thugs</td><td class='stats'>Dealers</td><td class='stats'>Drugs</td><td class='stats'>Networth</td>  </tr>";$rank = 1;$query = mysql_query("SELECT username, dealers, thugs, drugs, networth FROM playerstats ORDER BY networth DESC   ");while($row = mysql_fetch_array($query)) {	$username= $row['username'];	$dealers= $row['dealers'];	$thugs= $row['thugs'];	$drugs= number_format($row['drugs']);	$networth= number_format($row['networth']);	echo "<tr><td class='stats'>$rank</td><td class='stats'>$username</td><td class='stats'>$dealers</td><td class='stats'>$thugs</td><td class='stats'>$drugs</td><td class='stats'>$networth</td> </tr>";	$rank++;}echo "</table>";

Link to comment
Share on other sites

ok the $rank variable was supposed ot be the one which increases each iteration of the loop, however need a variable of a similar kind that I can match each iteration to, so that I can display each user in order of networth. What I cant figure out is how to link these iterations to the accounts in the right order.When using loops for other actions i simply matched the id numbers users get when they sign up with the variable that increases on iterations, this is not as easy to do when they need to be listed in a certain order.

Link to comment
Share on other sites

yeah, the plan is to match it to $rank during iterations. but I cant think of a way to link each iteration to the users in the order of networth to get the variable to match it.

Link to comment
Share on other sites

OK, you'll just need to count the amount of rows.

$q = mysql_query('SELECT COUNT(*) FROM playerstats');if($r = mysql_fetch_row($q)) {  $networthvalue = $r[0];} else {  $networthvalue = 0;}$rank = 1;$query = mysql_query("SELECT username, dealers, thugs, drugs, networth FROM playerstats ORDER BY networth DESC   ");while($row = mysql_fetch_array($query)) {	$username= $row['username'];	$dealers= $row['dealers'];	$thugs= $row['thugs'];	$drugs= number_format($row['drugs']);	$networth= number_format($row['networth']);	// Use $networthvalue wherever you need it	echo "<tr><td class='stats'>$rank</td><td class='stats'>$username</td><td class='stats'>$dealers</td><td class='stats'>$thugs</td><td class='stats'>$drugs</td><td class='stats'>$networth</td> </tr>";	$rank++;	$networthvalue--;}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...