duanfu1945 2015-06-18 23:32
浏览 132

当我使用PHP echo时,Font Awesome图标无法渲染?

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>';
}
  • 写回答

1条回答 默认 最新

  • duanba3707 2015-06-19 00:01
    关注

    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';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line