I’m trying to make a tracking program for an online game, and I need to sort gained experience by whoever has gained the most experience.
However, what happens is, say I’m tracking the users (a, b, c, d, e, f, g), and the users (b, e, g) have gained 0 experience. It will show (b) 3 times, because there is 3 people that have gained 0 experience and (b) is the first person, out of 3, that have gained 0 experience. I can’t think of how to show you just what I’m doing without the actual file, so here’s the file’s contents:
<?php
$participants = array("Quuxx", "Aeterna", "Ts Danne", "Marsman", "PFC Mage");
$skills = array("overall", "attack", "defense", "strength", "constitution", "ranged", "prayer", "magic", "cooking", "woodcutting", "fletching", "fishing", "firemaking", "crafting", "smithing", "mining", "herblore", "agility", "thieving", "slayer", "farming", "runecrafting", "hunter", "construction", "summoning", "dungeoneering");
$database = mysql_connect("mysql.alwaysdata.com", "*", "*");
if (!$database) {
die('Could not connect to database: ' . mysql_error());
}
mysql_select_db("tracker_tkoblitz", $database);
if (isset($_GET['track'])) {
for ($i = 0; $i < count($participants); $i++) {
startTracker($participants[$i]);
}
}
shit(array_search($_GET['skill'], $skills));
function startTracker($username) {
$stats = getStats($username);
mysql_query("INSERT IGNORE INTO stats (username, stats) VALUES ('$username', '$stats')");
}
function grabStats($username) {
$query = mysql_query("SELECT * FROM stats WHERE username LIKE '$username'");
while ($row = mysql_fetch_array($query)) {
return $row['stats'];
}
}
function shit($lol) {
global $participants, $skills;
sort($participants);
echo '<head><title>Fight Tracker</title><link rel="stylesheet" href="style.css"></head><body><div align="center"><img src="http://www.runehead.com/clans/banners/clansolace-6617078.png"><table id="atable"><thead><tr><th scope="col">Username</th><th scope="col">Skill</th><th scope="col">Level</th><th scope="col">Starting experience</th><th scope="col">Ending experience</th><th scope="col">Gained experience</th></tr></thead>';
for ($i = 0; $i < count($participants); $i++) {
$level = getStat(grabStats($participants[$i]), 1, $lol);
$starting = getStat(grabStats($participants[$i]), 2, $lol); //2nd param: rank,lvl,xp
$stats = getStats($participants[$i]); //2nd param: rank,lvl,xp
$current = getStat($stats, 2, $lol);
$gained[] = $current - $starting;
//echo '<tr><td class="odd">' . $participants[$i] . '</td><td class="odd"><img src="images/' . $skills[$lol] . '.png"></td><td class="odd">' . number_format($level) . '</td><td class="odd">' . number_format($starting) . '</td><td class="odd">' . number_format($current) . '</td><td class="odd">' . number_format($gained) . '</td></tr>';
$statss[] = array($participants[$i], $level, $starting, $current, $gained[$i]);
}
arsort($gained);
foreach ($gained as $gain) {
$skillData = getSkillData($gain, $statss);
$name = $skillData[0];
$level = $skillData[1];
$start = $skillData[2];
$current = $skillData[3];
$gainedXP = $skillData[4];
echo '<tr><td class="odd">' . $name . '</td><td class="odd"><img src="images/' . $skills[$lol] . '.png"></td><td class="odd">' . number_format($level) . '</td><td class="odd">' . number_format($start) . '</td><td class="odd">' . number_format($current) . '</td><td class="odd">' . number_format($gainedXP) . '</td></tr>';
}
echo '</tbody></table></div><br><form name="skill_select" action="view_tracker.php" method="GET"><div align="center"><font color="#03F">View a different skill: </font><select name="skill"><option value="overall">Overall</option><option value="attack">Attack</option><option value="defence">Defence</option><option value="strength">Strength</option><option value="constitution">Constitution</option><option value="ranged">Ranged</option><option value="prayer">Prayer</option><option value="magic">Magic</option><option value="cooking">Cooking</option><option value="woodcutting">Woodcutting</option><option value="fletching">Fletching</option><option value="fishing">Fishing</option><option value="firemaking">Firemaking</option><option value="crafting">Crafting</option><option value="smithing">Smithing</option><option value="mining">Mining</option><option value="herblore">Herblore</option><option value="agility">Agility</option><option value="thieving">Thieving</option><option value="slayer">Slayer</option><option value="farming">Farming</option><option value="runecrafting">Runecrafting</option><option value="hunter">Hunter</option><option value="construction">Construction</option><option value="summoning">Summoning</option><option value="dungeoneering">Dungeoneering</option></select> <input type="submit" value="View" /></div></form></body>';
}
function read($file) {
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
for ($c = 0; $c < count($data); $c++) {
$lines[] = $data[$c];
}
}
fclose($handle);
}
return $lines;
}
function getSkillData($gain, $stats) {
foreach ($stats as $stat) {
if ($stat[4] == $gain) {
return $stat;
}
}
}
function getStats($username) {
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://hiscore.runescape.com/index_lite.ws?player=" . $username);
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0", rand(4, 5)));
curl_setopt ($curl, CURLOPT_HEADER, (int) $header);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl, CURLOPT_VERBOSE, 1);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$output = curl_exec($curl);
curl_close ($curl);
if (strstr($output, "<html><head><title>")) {
return false;
}
return $output;
}
function getStat($stats, $row, $skill) {
$stats = explode("\n", $stats);
$levels = explode(",", $stats[$skill]);
return $levels[$row];
}
mysql_close($database);
?>
Here’s a live demo of that same exact file: http://tracker.alwaysdata.net/solace/view_tracker.php
Notice PFC Mage is there twice, when it should show PFC Mage and Quuxx separately. Everything else sorts just fine, any ideas?
Problem is here:
This function returns first element that satisfies condition. So, if you have two or more people with equivalent exp you always get only the first. Consider passing another parameter, like nth-occurence or startIndex, like this: