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 ", ";
					}
				}
				?>

Dani AI

Generated

The original loop in 's post mis-uses end($array) (that returns the last element value, not "am I on the last row") and also relies on the old mysql_* extension. Modern code should avoid that extension (removed in PHP 7) and must escape HTML output to prevent XSS. See the PHP migration notes for removal of ext/mysql and the htmlspecialchars docs for safe output (migration notes, htmlspecialchars).

A simple, robust pattern is to fetch only the needed columns, build an array of formatted link strings (apply htmlspecialchars, cast IDs to int), then implode(', ', $links) to print with commas only between items. Below is a safe, modern example using PDO (keeps the highlighting logic but avoids deprecated APIs):

<?php
$pdo = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8mb4','user','pass',[
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$stmt = $pdo->query("SELECT id, username, level, banned FROM users WHERE online = 1 ORDER BY username");
$links = [];
while ($r = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $name = htmlspecialchars($r['username'], ENT_QUOTES, 'UTF-8');
    $id   = (int)$r['id'];
    if ($r['banned']) {
        $links[] = "<del><b><a href='User.php?ID={$id}'>{$name}</a></b></del>";
    } elseif (in_array($r['username'], ['Chosen','CoderRyne'], true)) {
        $links[] = "<b><a href='User.php?ID={$id}' class='special'>{$name}</a></b>";
    } elseif ($r['level'] == 9) {
        $links[] = "<b><a href='User.php?ID={$id}' class='mod'>{$name}</a></b>";
    } else {
        $links[] = "<b><a href='User.php?ID={$id}'>{$name}</a></b>";
    }
}
echo implode(', ', $links);

Comments on the thread: 's "prefix/comma before next item" approach is fine for streaming output; 's implode idea is the cleanest when formatting is built into strings; 's counting works but depends on deprecated functions here. Prefer CSS classes over <font> tags, enable PDO/MySQL errors during development, and test encoding to avoid broken characters.

Recommended Answers

All 3 Replies

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 Member #334542

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.