Jump to content

[SOLVED] showing a list .. 1,2,3,4 and stoping a comma been added on the end?


runnerjp

Recommended Posts

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?

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);
}
?>

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);

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

Yo Jaret :P 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 ^^;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.