runnerjp Posted October 28, 2008 Share Posted October 28, 2008 im shoing a list of users print "<A href='$getusersonline3[user]'>$getusersonline3[user]</a>,"; and i feel it would look nicer if the comma was not on the end of the last one... i know i could use an if statement to show just a name if there is just one user but what if there is more... how would i prevent the comma been shown? Link to comment https://forums.phpfreaks.com/topic/130456-solved-showing-a-list-1234-and-stoping-a-comma-been-added-on-the-end/ Share on other sites More sharing options...
rhodesa Posted October 28, 2008 Share Posted October 28, 2008 i assume this is in some loop... <?php $users = array(); foreach(...loop stuff here...){ $users[] = "<a href='$getusersonline3[user]'>$getusersonline3[user]</a>"; } print implode(',',$users); } ?> Link to comment https://forums.phpfreaks.com/topic/130456-solved-showing-a-list-1234-and-stoping-a-comma-been-added-on-the-end/#findComment-676736 Share on other sites More sharing options...
runnerjp Posted October 28, 2008 Author Share Posted October 28, 2008 nope no loop... full code is as shows.. <?php $getusersonline="SELECT user_id,user FROM useronline WHERE file = 'http://www.runningprofiles.com/members/index.php?page=forum&forum=$forum' AND timestamp > " . (time() - 900) ; //grab from sql users on in last 15 minutes $getusersonline2=mysql_query($getusersonline) or die("Could not get users"); $num=mysql_num_rows($getusersonline2); echo "<b>There " . ($num != 1 ? "are" : "is") . " $num user" . ($num != 1 ? "s" : "") . " currently viewing the $forum board: </B>"; while($getusersonline3=mysql_fetch_array($getusersonline2)) { print "<A href='$getusersonline3[user]'>$getusersonline3[user]</a>,"; } ?> would there be a better way of doing this? also would i just add print implode(',',$users); Link to comment https://forums.phpfreaks.com/topic/130456-solved-showing-a-list-1234-and-stoping-a-comma-been-added-on-the-end/#findComment-676739 Share on other sites More sharing options...
kenrbnsn Posted October 28, 2008 Share Posted October 28, 2008 As rhodesa said, just replace: <?php while($getusersonline3=mysql_fetch_array($getusersonline2)) { print "<A href='$getusersonline3[user]'>$getusersonline3[user]</a>,"; } ?> with <?php $tmp = array(); while($getusersonline3=mysql_fetch_array($getusersonline2)) { $tmp[] = "<A href='$getusersonline3[user]'>$getusersonline3[user]</a>"; } echo implode(',',$tmp); ?> Ken Link to comment https://forums.phpfreaks.com/topic/130456-solved-showing-a-list-1234-and-stoping-a-comma-been-added-on-the-end/#findComment-676741 Share on other sites More sharing options...
kmaid Posted October 28, 2008 Share Posted October 28, 2008 Yo Jaret guess who! Just load all the userids into an array and then use the imlpode function. while($getusersonline3=mysql_fetch_array($getusersonline2)) { $UserIDs = $getusersonline3[user]; } echo implode(',',$UserIDs ); edit Haha, I was beaten twice and missed the []s off UserIDs ^^; Link to comment https://forums.phpfreaks.com/topic/130456-solved-showing-a-list-1234-and-stoping-a-comma-been-added-on-the-end/#findComment-676743 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.