Jump to content

[SOLVED] How do you "Display Users Current Rank In Database"


affc

Recommended Posts

Hello all,

 

I have tried this many ways using the $Rank=0 code which will only display number 1 for every user that logs in. Is this possible in anyway to still only have the User display there row only but still have their overall rank compared to all the other members displayed?

 

Thanx guys/girls

 

<php
$sql="SELECT *, Goals+Points AS Total FROM scoreboard ORDER BY Total DESC WHERE Member='$Memberloginname'";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
$Rank = 0;
?>
Hello <? echo $rows['Member']; ?>, your current Rank out of everyone is <? echo ++$Rank; ?>
<?php
mysql_close();
?>

Link to comment
Share on other sites

I use this code to rank members hourly using a cron job.

 

<?php

/*** Work out user ranks ***/

$query = mysql_query("SELECT * FROM cf_users");

while($row = mysql_fetch_array($query)){ // grab database info for each user
// edit the column names below
$add = $row['strikeaction'] + $row['defenceaction'] + $row['covertaction'];
$result = $add / 3; // divide by number of columns to get the average score
$user = $row['id']; // grab user id
mysql_query("UPDATE cf_users SET score = '$result' WHERE id = '$user'"); // update the users score(average)
}

$x = 1; // first rank is 1
$select = mysql_query("SELECT * FROM cf_users ORDER BY score DESC"); // grab all users with there new scores
while($var = mysql_fetch_array($select)){
$person = $var['id']; // grab the users ids again
mysql_query("UPDATE cf_users SET rank = '$x' WHERE id = '$person'"); // give each user there rank
$x++; // ranks 1, 2, 3 etc...
}

?>

 

You need a score integer column in your database for this to work.

 

Regards ACE

Link to comment
Share on other sites

Thanx for the reply. So i guess there is not other way of doing it so that it just querys the Database and spits back what their Current Rank is?

 

+: I dont need the Scoreboard to update any of the members details when they view it through their login, just need it to let them know what their current rank is. Not sure how this could be done really, but their scores are updated via other pages through other means.

Link to comment
Share on other sites

The score is stored in the Total. I can get the Rank if I display all of the members at once and just echo $Rank but when I limit it to only display 1 member it always displays that member as Rank 1. Thought there might be a way around it?

Link to comment
Share on other sites

So you want rankings based on Goals+Points? Try this:

 

$sql = "SELECT *,Goals+Points as score, (SELECT COUNT(*)+1 FROM scoreboard WHERE Goals+Points > score) as rank FROM scoreboard Member='$Memberloginname'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo 'Your rank is: '.$row['rank'];

Link to comment
Share on other sites

This displays each member and their current Rank. Is their a way just to have it remove all the other rows and just display the row of the Member?

<?php
$sql="SELECT *, Goals+Points AS Total FROM scoreboard ORDER BY Total DESC";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
$Rank = 0;
?>
<? echo $rows['Member']; ?> is currently rank = <? echo ++$Rank; ?>
<?php
mysql_close();
?>

Link to comment
Share on other sites

So you want rankings based on Goals+Points? Try this:

 

$sql = "SELECT *,Goals+Points as score, (SELECT COUNT(*)+1 FROM scoreboard WHERE Goals+Points > score) as rank FROM scoreboard Member='$Memberloginname'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo 'Your rank is: '.$row['rank'];

 

GingerRobots query should do it if you fix it by adding "WHERE" before the " Member='$Memberloginname' "

Link to comment
Share on other sites

So you want rankings based on Goals+Points? Try this:

 

$sql = "SELECT *,Goals+Points as score, (SELECT COUNT(*)+1 FROM scoreboard WHERE Goals+Points > score) as rank FROM scoreboard Member='$Memberloginname'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo 'Your rank is: '.$row['rank'];

 

GingerRobots query should do it if you fix it by adding "WHERE" before the " Member='$Memberloginname' "

 

Whoops. Good spot.

Link to comment
Share on other sites

Just had to add the WHERE in the sql but working perfectly, if only everyone could have seen me jumping into the sky over this code, you make it look to easy, thanx again and for anyone else that needs a working code to display their Rank use the code below works a charm, all thanx to Ginger Robot

$sql = "SELECT *,Goals+Points as score, (SELECT COUNT(*)+1 FROM scoreboard WHERE Goals+Points > score) as rank FROM scoreboard WHERE Member='$Memberloginname'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo 'Your rank is: '.$row['rank'];

:D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.