>我正在尝试设置一个菜单列表,该列表从mysql查询中提取一个数组并使用foreach来回显每个列表元素。
我正在使用字体真棒,由于某种原因,当我将<i>
元素放在回声线内时,图标不会呈现。同一页面上的其他图标呈现良好。
我已经验证了所有CSS
文件都已正确包含。
这是代码块,您可以看到我正在使用str_replace()
生成一些图标名称,但是回显中还有其他静态图标。
我在这里拔头发。
$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>';
}
如果$icon
包含"外部"以外的任何内容,则$row['type']
使用无效的图标类字符串重新分配。
假设$row['type']
(不要忘记为数组键使用引号)包含"内部"。后
$icon = str_replace("Internal", "fa-user", $row['type']);
$icon
将是"fa-user"。然后,之后
$icon = str_replace("External", "fa-user-times", $row['type']);
$icon
将是"内部"。
假设$row['type']
可能只是"内部"或"外部",我会使用这样的东西
$icon = $row['type'] == 'Internal' ? 'fa-user' : 'fa-user-times';
或者,如果您有其他类型,则可以使用 switch 语句
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';
}