How would I go about doing that? Here's my script:

<?php
				$q = mysql_query("SELECT * FROM users WHERE online = '1'");
				
				while($array = mysql_fetch_array($q)) {
					if($array['banned']) {
						echo "<del><b><a href='User.php?ID=$array[id]'>$array[username]</a></b></del>";
					} elseif($array['username'] == "Chosen" || $array['username'] == "CoderRyne") {
						echo "<b><a href='User.php?ID=$array[id]'><font color='red'>$array[username]</font></a></b>";
					} elseif($array['level'] == 9) {
						echo "<b><font color='orange'><a href='User.php?ID=$array[id]'>$array[username]</a></font></b>";
					} else {
						echo "<b><font color='lightgrey'><a href='User.php?ID=$array[id]'>$array[username]</font></a></b>";
					}
					
					$id = $array['id'];
					if(!$array[end($array)]) {
						echo ", ";
					}
				}
				?>

Multiple ways to do this. Try making a count, that increments whenever a user is displayed. Then each time, if the count is greater than 0(i.e. at least one user has been displayed) display ", " before the username. Theoretical example:

$count = 0;
while($user = mysql_fetch_array($result)) {
// Code to check for ban or w/e here
if($count) {
echo ", ";
}
// display user here
}

A side benefit of the count is that you can display the number of online users :)

Another way of doing this might be to load all usernames into an array, then "implode" the array using a comma separator.

$users = array();
while($user = mysql_fetch_array($result))
   $users[] = $user;

// ensure that there is something in the array
if (is_array($users) && count($users) > 0)
   echo implode(",", $users);

As Lsmjudoka said, there are many ways to do what you want.

Member Avatar for rajarajan2017

Its simple...

$query = "SELECT * FROM users WHERE online = '1'"; 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

$len = mysql_num_rows($result);

if (mysql_num_rows($result) > 0) { 
	$count = 0;
	while ($row = mysql_fetch_row($result)) { 
	    if ($count!=$len-1){
			echo $row[0] . ',';
			$count++;
		}
		else echo $row[0];
		
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.