Aeterna Posted August 8, 2011 Share Posted August 8, 2011 Hi guys, 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 it is: <?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? Quote Link to comment https://forums.phpfreaks.com/topic/244169-arsort-problem/ Share on other sites More sharing options...
@ Posted August 13, 2011 Share Posted August 13, 2011 The issue is that your getSkillData function returns the first match in the statss array. I think you may need to re-work the way the you build your data in order to fix it - maybe something like this: :page file <?php require_once('leaderboard_functions.php'); $initialise = false; $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'); if(isset($_GET['track'])) { $initialise = true; } $skillIndex = empty($_GET['skillIndex']) ? 0 : $_GET['skillIndex']; $leaderboard = generateLeaderboard($skillIndex, $initialise); ?> <!DOCTYPE HTML> <html> <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> <tbody> <?php foreach($leaderboard as $player => $stats) { print '<tr>'; print '<td class="odd">'.$player.'</td>'; print '<td class="odd">'; print '<img src="images/'.$skills[$skillIndex].'.png">'; print '</td>'; print '<td class="odd">'.number_format($stats['level']).'</td>'; print '<td class="odd">'.number_format($stats['startingExperience']).'</td>'; print '<td class="odd">'.number_format($stats['currentExperience']).'</td>'; print '<td class="odd">'.number_format($stats['experienceGained']).'</td>'; print '</tr>'; } ?> </tbody> </table> </div> <br> <form name="skill_select" action="" method="GET"> <div align="center"> <font color="#03F">View a different skill: </font> <select name="skillIndex"> <?php foreach($skills as $key => $skill) { print '<option value="'.$key.'">'.$skill.'</option>'; } ?> </select> <input type="submit" value="View" /> </div> </form> </body> </html> :functions file <?php function connectToDatabase() { $database = mysql_connect("mysql.alwaysdata.com", "*", "*"); if(!$database) { die('Could not connect to database: ' . mysql_error()); } mysql_select_db("tracker_tkoblitz", $database); return $database; } function startTracker($username, $stats) { $query = " INSERT INTO stats ( username, stats ) VALUES ( '".mysql_real_escape_string($username)."', '".mysql_real_escape_string($stats)."' )"; mysql_query($query); } function grabStats($username) { $query = " SELECT stats FROM stats WHERE username = '".mysql_real_escape_string($username)."' LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_array($query); return $row['stats']; } function generateLeaderboard($participants, $skillIndex, $initialise) { $database = connectToDatabase(); $participants = array('Quuxx', 'Aeterna', 'Ts Danne', 'Marsman', 'PFC Mage'); $leaderboard = array(); //sort in reverse as the uasort below will reverse the order rsort($participants); foreach($participants as $key => $name) { $currentStats = getStats($name); if($initialise) { //start the tracker for each participant as we go startTracker($name, $userStats); } //fetch saved statistics from the database $startingStats = grabStats($name); //set the rank level and experience for this skill from the database list($startingRank, $startingLevel, $startingExperience) = getSkillAttributes($startingStats, $skillIndex); //set the rank level and experience for this skill from the current data list($currentRank, $currentLevel, $currentExperience) = getSkillAttributes($currentStats, $skillIndex); //calcuate the experience gained $gainedExperience = $currentExperience - $startingExperience; //add the data to an array $leaderboard[$name] = array('level' => $currentLevel, 'startingExperience' => $startingExperience, 'currentExperience' => $currentExperience, 'experienceGained' => $gainedExperience); } //sort the leader board by gained experience uasort($leaderboard, 'compare'); mysql_close($database); return $leaderboard; } function compare($a, $b) { if ($a['experinceGained'] == $b['experinceGained']) { return 0; } return ($a['experinceGained'] < $b['experinceGained']) ? -1 : 1; } 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 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 getSkillAttributes($stats, $index) { $skills = explode("\n", $stats); $skill = explode(',', $skills[$index]); return $skill; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244169-arsort-problem/#findComment-1256706 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.