我正在为一个在线游戏做一个跟踪程序,我需要把获得的经验分类给那些获得最多经验的人。
但是,如果我跟踪用户(a,b,c,d,e,f,g),用户(b,e,g)获得了0次体验。它将显示(b)3次,因为有3个人获得了0次经验,(b)是第一个人,在3个人中,获得了0次经验。如果没有实际的文件,我想不出如何向你展示我正在做的事情,所以文件的内容如下:

<?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);

?>

这是同一个文件的现场演示:http://tracker.alwaysdata.net/solace/view_tracker.php
请注意,PFC Mage已经存在两次了,它应该分别显示PFC Mage和quouxx。其他的都很好,有什么想法吗?

最佳答案

问题在于:

foreach ($stats as $stat) {
    if ($stat[4] == $gain) {
        return $stat;
    }
}

此函数返回满足条件的第一个元素。所以,如果你有两个或两个以上有相同经验的人,你总是只得到第一个。考虑传递另一个参数,如nth occurrence或startIndex,如下所示:
function getSkillData($gain, $stats, $nthOccurence=1) {
    $occurence = 0;
    foreach ($stats as $stat) {
        if ($stat[4] == $gain) {
             ++$occurence;
             if ($occurence == $nthOccurence)
                  return $stat;
        }
    }
}

10-08 02:21