//check which faction members are online
$sql = mysql_query("SELECT * FROM ".TBL_ACTIVE_USERS."
WHERE faction=$userfaction_id ORDER BY timestamp DESC,username");
//no '' around var as it is an integer, so php doesn't expeect it to be string
$numrows = mysql_numrows($sql);//gets number of members online
if($numrows == 1){ echo 'You are the only faction member online'; }
else{
while($online = mysql_fetch_array($sql)){
echo '<a href="#" class="light_grey">'.$online['username'].'</a>';
echo ', ';
}//loops round all online users
//echoing their usernames
}
Run Code Online (Sandbox Code Playgroud)
如果只有一个成员在线,上面的代码工作正常.问题实际上是美学原因.
如果有多个成员在线,则查询显示:
管理员,系统,
我想知道如何在最后的结果(最后一个成员在线由while(){}子句)这样做我可以删除逗号吗?有没有办法将while语句限制为$ numrows-1或其他类似的东西?然后在名字后面没有逗号和空格的最后一个用户回显?
一种优雅的方法是使用数组和implode().
$elements = array();
while($online = mysql_fetch_array($sql)){
array_push ($elements, '<a href="#" class="light_grey">'.
$online['username'].'</a>');
}
echo implode(", ", $elements);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
137 次 |
| 最近记录: |