Jump to content

PHP Help - Simple Ranking System


Cory_

Recommended Posts

Hello,

 

I'm in need of assistance trying to get this PHP Script working and I'm hoping you'll be able to find a solution.

 

SQL Table Format: http://pastebin.com/LUuu2pLp

--
-- Table structure for table `hungergames_records`
--

CREATE TABLE IF NOT EXISTS `hungergames_records` (
  `user_id` int(10) unsigned NOT NULL,
  `victories` int(32) unsigned NOT NULL DEFAULT '0',
  `biggest_kill_streak` int(32) unsigned NOT NULL DEFAULT '0',
  `most_chests_opened` int(32) unsigned NOT NULL DEFAULT '0',
  `total_chests_opened` int(32) unsigned NOT NULL DEFAULT '0',
  `lastlogin` int(32) unsigned NOT NULL DEFAULT '0',
  `longest_lifespan` int(32) unsigned NOT NULL DEFAULT '0',
  `total_lifespan` int(32) unsigned NOT NULL DEFAULT '0',
  `total_points` int(32) unsigned NOT NULL DEFAULT '100',
  `most_points` int(32) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `hungergames_users`
--

CREATE TABLE IF NOT EXISTS `hungergames_users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `donor` int(32) unsigned NOT NULL DEFAULT '0',
  `user` varchar(40) NOT NULL,
  `donor_cooldown` int(100) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `user` (`user`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12641 ;

 

PHP Code: http://pastebin.com/aQANz0BL

<?php

include("config.php");

$getRankQuery = "SELECT hungergames_users.id, hungergames_records.user_id, hungergames_records.victories, hungergames_records.biggest_kill_streak, hungergames_records.most_chests_opened, hungergames_records.total_chests_opened, hungergames_records.lastlogin, hungergames_records.longest_lifespan, hungergames_records.total_lifespan, hungergames_records.total_points, hungergames_records.most_points, hungergames_users.donor, hungergames_users.user FROM hungergames_records, hungergames_users WHERE hungergames_records.user_id = hungergames_users.id ORDER BY hungergames_records.total_points DESC";
$getRankResult =  mysql_query($getRankQuery);

//Source From: http://stackoverflow.com/questions/7704311/calculate-rank-with-points 
function getUserRank($searchedUserID)
{
    $userArray = getAllUsersOrderedByPoints();
    $rank       = 0;
    $lastPoints = -1; // Never happens
    foreach ( $userArray as $user) {
        if ($user['total_points'] != $lastPoints)  $rank++;
        if ($user['user_id'] == $searchedUserID) break;
    }
    return $rank;
}

while ($data = mysql_fetch_array($result)) {
echo getUserRank($data["user_id"]);
}

?>

 

What am I trying to accomplish? Simple Ranking System...

 

Basically, The total points is stored into total_points field for each user and whoever has the highest score will be Ranked #1 and count down to a the full database (Currently at 12,641 users)

 

Now the above function getUserRank is the code I pulled from the above source listed, I think my issue relies in the function of getAllUsersOrderedByPoints(); - I've tried doing something like.

 

$row = mysql_fetch_array($getRankResult);

Then adding:

    $userArray = $row["user_id"];

 

Didn't work - So if you can help me correct this than I'll be greatly appertiated.

 

Lastly, I've tried doing:

 

$index = 1;
  while ($data = mysql_fetch_array($getRankQuery)) {
    echo $index++ . " " . $data["id"] . " ".$data["user"]." <br>\n";
}

 

This solution works, but this solution would not work for when a "User is Search" it would count them as the highest rank 1,2,3 and wouldn't work, so either the function up there needs to be corrected or please help get a working solution, I've been googleing this issue and seems too be a common code issue with a lot people are having and not many solutions.

 

The application that records data (Not PHP) doesn't store any data with a "Rank" table otherwise this would be a simple echo'ed out data.

 

For a better understanding of what I'm trying to do - You may take a look at: http://justonemoreblock.com/lb/leaderboards.php

 

Regards,

Cory

Link to comment
Share on other sites

Does the below produce what you want? And if not then copy-paste (or link) the result.

 

$sql = '
  SELECT   hungergames_users.id, hungergames_records.user_id, hungergames_records.victories, hungergames_records.biggest_kill_streak,
           hungergames_records.most_chests_opened, hungergames_records.total_chests_opened, hungergames_records.lastlogin,
           hungergames_records.longest_lifespan, hungergames_records.total_lifespan, hungergames_records.total_points, hungergames_records.most_points,
           hungergames_users.donor, hungergames_users.user
  FROM     hungergames_records
  JOIN     hungergames_users ON hungergames_users.id = hungergames_records.user_id
  ORDER BY hungergames_records.total_points DESC
';

$result = mysql_query($sql, $mysql_link);
if ($result && mysql_num_rows($result)) {
    $rank = 1;
    while ($row = mysql_fetch_array($result)) {
        echo ($rank++), ' ', $row['id'], ' ', $data['user'], '<br>', PHP_EOL;
    }
}

Link to comment
Share on other sites

Ok, If you goto: http://justonemoreblock.com/lb/leaderboards.php

 

You'll see the user rank as the highest number for everyone: 12640

 

if ($getRankResult && mysql_num_rows($getRankResult)) {
    $rank = 1;
    while ($row = mysql_fetch_array($getRankResult)) {
        $getUserRank = "".$rank++."";
    }
}

 

This is the code I'm using because I need this to be a variable, $getUserRank = "".$rank++."";

 

So I can place it in this code:

 

while ($data = mysql_fetch_array($result)) {
print "<tr align=\"center\">
    <td>$getUserRank</td>
    <td $donor>".$data["user"]."</td>
    <td>".$data["total_points"]."</td>
    <td>".$data["victories"]."</td>
    <td>".$data["most_points"]."</td>
    <td>".$data["biggest_kill_streak"]."</td>
    <td>".$data["longest_lifespan"]."</td>
    <td>".$data["total_lifespan"]."</td>
    <td>".$data["most_chests_opened"]."</td>
    <td>".$data["total_chests_opened"]."</td>
    <td>".date('m/d/Y', $data["lastlogin"])."</td>
  </tr>";
}

 

This is another while...

Link to comment
Share on other sites

Now you are just pissing me off! I wrote you some example code and asked you to try it out which would have let to you examining the code as to why mine works and yours dont. But instead you ignored my effort and went ahead with no clue of what you are doing or what is wrong.

 

Everyone on this forum who glances over your code can tell you why everyone has the same rank, but I'm now just refusing to tell you because you showed no respect whatsoever!

Link to comment
Share on other sites

Now you are just pissing me off! I wrote you some example code and asked you to try it out which would have let to you examining the code as to why mine works and yours dont. But instead you ignored my effort and went ahead with no clue of what you are doing or what is wrong.

 

Everyone on this forum who glances over your code can tell you why everyone has the same rank, but I'm now just refusing to tell you because you showed no respect whatsoever!

 

How am I pissing you off? I used the sample code you provided me and works fine "Standalone", but can't work standalone, needs to be integrated into the script and the way you've done it doesn't work.

 

I've tried in closing it in a function with a return value, used a .= - All errors, I did this and some other methods before posting.

 

I don't ignore your effort, but the code you provided is slightly different and had thought of using num rows myself, but wasn't sure how to implement it.

 

I'm a PHP Coder, but I've been out of practice for awhile and I do need a refresher course.

 

Well now they won't because you're respected member and of course they would take your word over mine as I'm new here, I give credit when it due and give thanks after it's done.

 

The thing is, I could posted a link too the code to show it works, but would know it already works - I'm skipping posts ahead I would like to get this accomplished, I do have respect, I didn't mean to offend you or anyone and will give credit when it's due, I do it all the time. In fact I add //comments in my code whereas no one else would and provide the fix afterwards as I go on forums for someone else looking for a solution and the "Solution" is hardly posted and a message stated "I got it" and than see posts "How did you get it?" and no replies afterwards, I hate that and with your help I can accomplish this and give the credit is due.

 

I'm not an ass - so I apologize if I've offended you in anyway.

 

With all that said, Would you please help me?

 

Regards,

Cory

Link to comment
Share on other sites

I used the sample code you provided me and works fine "Standalone", but can't work standalone, needs to be integrated into the script and the way you've done it doesn't work.

 

Of course, I didn't intend to write the entire script for you so you could just copy-and-paste it and learn nothing in the process.

 

For example in your current code you use $getUserRank why? After the first while() $getUserRank will contain the lowest possible rank (12640), since the value is constantly overwritten. So at the second loop $getUserRank will be 12640 not 1,2,3,4,..

 

Just do the same thing as you did with $rank for $getUserRank and move it to your second while and be sure to enclose the $rank++ between parenteses and you'll find that it will now correctly display the rank (and remove the first while() loop altogether, you don't need it!).

Link to comment
Share on other sites

I used the sample code you provided me and works fine "Standalone", but can't work standalone, needs to be integrated into the script and the way you've done it doesn't work.

 

Of course, I didn't intend to write the entire script for you so you could just copy-and-paste it and learn nothing in the process.

 

For example in your current code you use $getUserRank why? After the first while() $getUserRank will contain the lowest possible rank (12640), since the value is constantly overwritten. So at the second loop $getUserRank will be 12640 not 1,2,3,4,..

 

Just do the same thing as you did with $rank for $getUserRank and move it to your second while and be sure to enclose the $rank++ between parenteses and you'll find that it will now correctly display the rank (and remove the first while() loop altogether, you don't need it!).

 

The problem I was having I needed the ($rank++) to work on the FrontPage and in search, they do, but when user searches using your solution it shows 1,2,3 and so on.

 

This is my solution to solve this, I've been saying it would of been easier if there was a classified field named rank - Well I created one.

 

Than using your code on PHP Script I used the mysql query update field to update that rank by that user_id.

 

Than with that, I'm able to define a varible to out my rank and the search works properly.

 

Now, I'm aware I'd have to run this on a cron job every 5 mins or an hour - but this is the simple way of getting this to work with your code.

 

The only reason why I use ". ." It's the way I was taught and I was self-taught, Infact the first thing I've installed was phpBB and I mainly coded on PHP-Nuke (CMS) for a really long time and just started modding scripts and taking them apart and what not, Afterwards - I tried successfully make my own code than there was a need for MySQL...which I admit I'm not great in - but I'm pretty smart if you got to know me.

 

I don't know why it took this long to make this solution the way I wanted it, just been really exhausted lately, so I didn't see it.

 

After cleaning the code a bit;

 

<?php

include("config.php");

$getRankQuery = "SELECT user_id, total_points FROM hungergames_records ORDER BY total_points DESC";
$getRankResult = mysql_query($getRankQuery);

if ($getRankResult && mysql_num_rows($getRankResult)) {
    $rank = 1;
    while ($row = mysql_fetch_array($getRankResult)) {
$getUserRank = mysql_query("UPDATE hungergames_records SET rank=".($rank++)." WHERE user_id=".$row["user_id"]."");
    }
}

if ($getUserRank) {
echo "User Ranks Updated - Successfully";
} else {
echo "Failed -"; mysql_error();
}

?>

 

I would like to say Thank you for your time and hope you'll be kind enough to help me in the near feature if needed.

 

Regards,

Cory

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.