I'm trying to set up a menu list which pulls an array from a mysql query and uses a foreach to echo out each list element.
I'm using font awesome, and for some reason when I place the <i>
elements inside of an echo line, the icons do not render. Other icons on the same page are rendering just fine.
I've verified that all of the CSS
files are being included properly.
Here is the block of code, you can see that I am generating some icon names using str_replace()
, however there are other icons in the echo that are static.
I'm pulling my hair out here.
$result = mysqli_query($con,"SELECT * FROM outageupdates ORDER BY timestamp");
while($row = mysqli_fetch_array($result))
{
$time = strtotime($row[timestamp]);
$time = date("H:i", $time);
$icon = str_replace("Internal", "fa-user", $row[type]);
$icon = str_replace("External", "fa-user-times", $row[type]);
echo '<li><a href="outageupdates.php"><i class="fa ' . $icon . '"></i>' . $row[agentname] . ' - ' . $row[type] . '<small class="pull-right"><i class="fa fa-clock"></i>' . $time . '</small></a></li>';
}
What's happening is you're re-assigning $icon
with an invalid icon class string if $row['type']
contains anything other than "External".
Say $row['type']
(and don't forget to use quotes for your array keys), contains "Internal". After
$icon = str_replace("Internal", "fa-user", $row['type']);
$icon
will be "fa-user". Then, after
$icon = str_replace("External", "fa-user-times", $row['type']);
$icon
will be "Internal".
Assuming $row['type']
may only be either "Internal" or "External", I'd use something like this instead
$icon = $row['type'] == 'Internal' ? 'fa-user' : 'fa-user-times';
Alternatively, you could use a switch statement if you have other types
switch($row['type']) {
case 'Internal':
$icon = 'fa-user';
break;
case 'External':
$icon = 'fa-user-times';
break;
case 'Admin':
$icon = 'fa-cogs';
break;
default:
$icon = 'fa-question-circle';
}