-2

我从链接获取来自 GT api (xml) 的所有数据:http ://api.gametracker.rs/demo/xml/server_info/217.26.212.10:20021/

特别是对于玩家列表,如您所见,所有 PID 均为 0,我使用以下代码在表格中显示它们:

<?php
foreach( $players as $player ) {
echo "
<ul>
<li id=\"number\">$player->pid.</li>
<li id=\"nickname\">$player->name</li>
<li id=\"score\">$player->score</li>
<li id=\"time\">$player->time</li>
</ul>";
}
?>

我能做些什么来让 pid 对 xml 文件中找到的玩家数量一样多?

4

2 回答 2

1
$i = 0; //Counter variable
foreach( $players as $player ) {
    $i++; //Increment the counter for each iteration
    echo "
        <ul>
        <li id=\"number\">$player->pid.</li>
        <li id=\"nickname\">$player->name</li>
        <li id=\"score\">$player->score</li>
        <li id=\"time\">$player->time</li>
        </ul>";
}
echo 'There were ' . $i . ' players in total';
于 2013-05-11T21:19:31.907 回答
0

您需要做的就是为此设置一个变量:

$i = 1;
foreach ($players as $player)
{
    echo "<ul>
            <li id=\"number-$i\">$i.</li>
            <li id=\"nickname-$i\">$player->name</li>
            <li id=\"score-$i\">$player->score</li>
            <li id=\"time-$i\">$player->time</li>
          </ul>";
    $i++;
}
于 2013-05-11T21:23:37.757 回答