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