I have developed a simple chat system. I am trying to make the admin color red to stand out among the chat users. My method takes the session username and if the session username is the admin then it turns everyone's text color red. How can I make it so only the admin color is red (everyone else is black)?
Here is my php code:
<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
if (isset($_SESSION['id'])) {
$userId = $_SESSION['id'];
$username = $_SESSION['username'];
$userLabel = $_SESSION['nickname'];
}
$connect = mysqli_connect("", "", "", "root");
mysqli_select_db($connect, "webclyde_root");
$result1 = mysqli_query($connect, "SELECT * FROM chat ORDER by id DESC");
while ($extract = mysqli_fetch_assoc($result1)) {
if ($username == "admin") {
echo "<br><b><font color='#D00003'>" . $extract['name'] . ":</b></font> " . $extract['message'];
}
else {
echo "<br><b><font color='#000000'>" . $extract['name'] . ":</b></font> " . $extract['message'];
}
}
?>
The font tag is not supported in HTML5. Use a CSS style attribute instead, for example:
echo "<br><b><p style='color:#D00003'>" . $extract['name'] . ":</b></p>" . $extract['message'];
Example for the if / else logic:
if($username=="admin"){
$color = "#D00003";
} else {
$color = "#000000";
}
echo "<p style='color:".$color."'>". $extract['name'] . "</p>";
Or use the statement RamRaider mentioned instead of the if / else statement:
$color = $username=='admin' ? 'red' :'black';
Also, you are checking if the session $username is 'admin'. I can imagine you want to check if the username from the database is 'admin'. In that case replace $username with $extract['name'] in the if statement.
Good luck!
replace your while statement with this:
while ($extract = mysqli_fetch_assoc($result1)) {
if ($username == "admin") {
$color = '#D00003';
}
else {
$color = '#000000';
}
echo "<br><b><font style='color:".$color."'>" . $extract['name'] . ":</b></font> " . $extract['message'];
}