0

我想为除最后一个用户名之外的每个用户名添加一个逗号。例如:User1、User2、User3 我该怎么做?这是我的代码:

$user_online_sql = $db->query("SELECT DISTINCT account_id FROM online WHERE account_id != '-1';");
$users = '';
while ($user_online_row = mysql_fetch_array($user_online_sql)) {
$users .= $user->name($user_online_row['account_id']) . ' ';
}

我尝试了 implode 函数,但它只输出了一个空白结果,这是带有 implode 函数的代码:

 $user_online_sql = $db->query("SELECT DISTINCT account_id FROM online WHERE account_id != '-1';");
$users = '';
while ($user_online_row = mysql_fetch_array($user_online_sql)) {
$users .= implode(', ',$user->name($user_online_row['account_id'])) . ' ';
} 
4

2 回答 2

5

你是对的使用implode,但你做错了。

$users = []; // or array() in PHP 5.3 and older
while($user_online_row = mysql_fetch_assoc($user_online_sql)) {
    $users[] = $user->name($user_online_row['account_id']);
}
$users = implode(", ",$users);
echo $users;
于 2013-05-19T23:03:51.443 回答
2

只需在每个用户后添加一个逗号,并删除尾随的逗号。

$user_online_sql = $db->query("SELECT DISTINCT account_id FROM online WHERE account_id != '-1';");
$users = '';
while ($user_online_row = mysql_fetch_array($user_online_sql)) {
$users .= $user->name($user_online_row['account_id']).',';
}
rtrim ($users, ",");
于 2013-05-19T23:04:04.520 回答