0

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 ", ";
					}
				}
				?>
4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by rajarajan07
0

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

Edited by Lsmjudoka: n/a

0

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.

0

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];
		
	}
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.