我得到它显示所有在线用户,但我将如何为最后一个在线用户制作它,最后去掉逗号。这是我的代码
while($online = mysql_fetch_array($usersonline)){
$username = $online['username'];
echo"<b> $username, </b>";
}
我建议使用内爆方法:
$usernames = [];
while($online = mysql_fetch_array($usersonline)){
array_push($usernames, $online['username']);
}
echo implode(',', $usernames);
只需对索引进行操作并在之前而不是之后追加,就像这样
$index = 0;
while($online = mysql_fetch_array($usersonline)) {
$username = $online['username'];
echo"<b> ".($index>0?',':'')." {$username}</b>";
$index++;
}
$u = 0;
while($online = mysql_fetch_array($usersonline))
{
$u++;
if($u == 1){
$usernames = '<b>' . $online['username'] . '</b>';
}else{
$usernames .= '<b>, ' . $online['username'] . '</b>';
}
}
编辑:内爆可能会更好。
$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);
这可能不是最优雅的方式,但这应该有效:
$users="<b>";
while($online = mysql_fetch_array($usersonline))
{
$username = $online['username'];
$users.="$username,";
}
rtrim($users,',');
$users.="</b>";
echo $users;