Hello friends,

I have a php page that displays data from a mysql DB. Fields are

1. Username
2. Designation
etc...

I want to list usernames in a listbox. I know thats not a tough job but I want to give different colors (or background color) to usernames with different designation.

ie. All usernames with designation "Teamleader" should have same color and "Manager" should have different color.

Can someone help me?...

Dani AI

Generated

A quick note: ’s inline-style trick is a perfectly fine quick fix and will work in many browsers, but styling of native <option> elements is limited and inconsistent across platforms (especially background colors, which are often controlled by the OS). Relying on inline colors alone can give different results on Windows vs macOS vs mobile. (developer.mozilla.org)

For a robust, modern approach, avoid the old mysql_* calls and output classes from a safe, parameterised query — then style those classes with CSS and escape user data to prevent XSS. Example (using PDO and safe output):

<?php
$stmt = $pdo->query('SELECT username, designation FROM users');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $cls = ($row['designation'] === 'Teamleader') ? 'teamlead'
       : (($row['designation'] === 'Manager') ? 'manager' : 'other');
  echo '<option value="' . htmlspecialchars($row['username'], ENT_QUOTES, 'UTF-8') .
       '" class="' . $cls . '">' . htmlspecialchars($row['username'], ENT_QUOTES, 'UTF-8') . '</option>';
}
?>

and CSS:

option.teamlead { color: #0066cc; }
option.manager  { color: #cc0000; }

Switching to PDO/MySQLi is recommended because the old ext/mysql was deprecated/removed; and always use htmlspecialchars() (or equivalent) when emitting user data. (wiki.php.net)

If you need full control (backgrounds, icons, richer markup), replace the native select with a custom control or a battle-tested library (Select2, Choices, Selectize/Tom Select) — they let you style items reliably and offer better UX for large lists. If you roll a custom listbox, follow WAI‑ARIA patterns so it remains accessible. Test across the browsers your users use and prefer the library or ARIA approach if visual styling must be consistent. (select2.org)

Troubleshooting tip: if a color “doesn’t work” in one browser, check whether that browser permits color but ignores background-color on <option>; if so, use a custom UI or add a small prefix marker (unicode glyph or separate label) to indicate role visually.

Recommended Answers

All 4 Replies

post your code

<?php
while($rows=mysql_fetch_array($result)){
echo "<option value=".$rows['username'].">".$rows['username']."</option>";
}?>

If $rows is "Teamleader" then the font color of $rows should set to blue color.

If $rows is "Manager" then the font color of $rows should set to red color.

I just want to highlight username in listbox according to respective designation.

Try this code.

<?php
while($rows=mysql_fetch_array($result))
{
	$style = '';
	if($rows['designation']=="Teamleader")
		$style = 'style="color:#0000FF;"';
	if($rows['designation']=="Manager")
		$style = 'style="color:#cc0000;"';
	
echo "<option value=".$rows['username']." ".$style.">".$rows['username']."</option>";
}
?>

Thanks "vibhadevit". It works perfect!..

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.