-1

我得到它显示所有在线用户,但我将如何为最后一个在线用户制作它,最后去掉逗号。这是我的代码

                    while($online = mysql_fetch_array($usersonline)){
                    $username = $online['username'];

                    echo"<b> $username, </b>";
                    }
4

5 回答 5

9

我建议使用内爆方法:

$usernames = [];

while($online = mysql_fetch_array($usersonline)){
  array_push($usernames, $online['username']);
}

echo implode(',', $usernames);
于 2012-09-16T21:03:36.217 回答
2

只需对索引进行操作并在之前而不是之后追加,就像这样

$index = 0;
while($online = mysql_fetch_array($usersonline)) {
  $username = $online['username'];
  echo"<b> ".($index>0?',':'')." {$username}</b>";
  $index++;
}
于 2012-09-16T21:04:11.300 回答
1
    $u = 0;
    while($online = mysql_fetch_array($usersonline))
    {
        $u++;
        if($u == 1){
            $usernames = '<b>' . $online['username'] . '</b>';
        }else{
            $usernames .= '<b>, ' . $online['username'] . '</b>';
        }
    }

编辑:内爆可能会更好。

于 2012-09-16T21:04:17.650 回答
0
$join = '';
while($online = mysql_fetch_array($usersonline))
{
    $username = $online['username'];
    echo"<b> $join . $username </b>";
    $join = ',';
}

或者

$out = '';
while($online = mysql_fetch_array($usersonline))
{
    $username = $online['username'];
    $out .="<b> $username </b>,";
}
$out = rtrim($out,",");
echo $out;

或作为“tehlulz”建议

$usernames = [];

while($online = mysql_fetch_array($usersonline)){
  array_push($usernames, $online['username']);
}

echo implode(',', $usernames);
于 2012-09-16T21:13:10.337 回答
0

这可能不是最优雅的方式,但这应该有效:

$users="<b>";
while($online = mysql_fetch_array($usersonline))
{
    $username = $online['username'];
    $users.="$username,";
}
rtrim($users,',');
$users.="</b>";
echo $users;
于 2012-09-16T21:04:43.640 回答