【问题标题】:Showing dummy pic if picture it not available如果图片不可用,则显示虚拟图片
【发布时间】:2018-03-28 19:47:54
【问题描述】:

当图片未设置时,我试图显示来自- Font awesome- 的虚拟图片。 你可以看到我的代码,如果有,它会显示图像,但是当没有时,它需要从awesomefont 设置虚拟图片。尝试将此 <i class="fas fa-user-circle"></i> 粘贴到 else 中,但这不起作用。谁能帮帮我?

   <?php
      $onderrwerp = $app->get_onderwerpen();
      foreach($onderrwerp as $onderwerp){
        echo '<div class="well well-sm">';
            if(file_exists('assets/images/profielfotos/'.$onderwerp['klant_id'])) { 
                echo '<img class="img-circle" src="/assets/images/profielfotos/'.$onderwerp['klant_id'].'/'.$onderwerp['foto'].'" />';
            } else {
            }
            echo '<b><a href="https://tom.lbmedia.nl/reactie"> '.$onderwerp['onderwerpnaam'].'</b></a>'; 
            echo ' - ' . $onderwerp['voornaam'] . ' ' . $onderwerp['achternaam'];
            echo '</div>';
        }
    ?>

更新 这对我有用!我用font-size 稍微改变了它。我唯一的问题是它没有很好地对齐。正如您在图片上看到的,第一个图片旁边的文字很好,但第二个和 3e 有点但更低?如何解决?

<script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl" crossorigin="anonymous"></script>
   <?php

      $onderrwerp = $app->get_onderwerpen();
      foreach($onderrwerp as $onderwerp){
        echo '<div class="well well-sm">';
            if(file_exists('assets/images/profielfotos/'.$onderwerp['klant_id'])) { 
                echo '<img class="img-circle" src="/assets/images/profielfotos/'.$onderwerp['klant_id'].'/'.$onderwerp['foto'].'" />';
            } else {
                echo '<i style="font-size: 2.5em;" class="fas fa-user-circle"></i>';
            }
            echo '<b><a href="https://tom.lbmedia.nl/reactie"> '.$onderwerp['onderwerpnaam'].'</b></a>'; 
            echo ' - ' . $onderwerp['voornaam'] . ' ' . $onderwerp['achternaam'];
            echo '</div>';
        }
    ?>

how it looks like now

【问题讨论】:

  • 好像是您的第三个帐户?
  • @u_mulder 也许可以,但也许我用临时邮件创建了 一个 其他帐户:# 但你知道如何解决这个问题吗?
  • 什么不起作用?
  • @LawrenceCherone 好吧,如果我将 &lt; i class="fa fa-user-circle"&gt;&lt;/i&gt; 添加到 else 它不会显示图片
  • 'assets/images/profielfotos/'.$onderwerp['klant_id'] 可能存在但$onderwerp['foto'] 不存在,您检查过它是否命中了 else?

标签: php html css image font-awesome


【解决方案1】:

您可能缺少所需的 js 文件,这就是它没有显示的原因。

Getting Started

试试:

<script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl" crossorigin="anonymous"></script>
   <?php

      $onderrwerp = $app->get_onderwerpen();
      foreach($onderrwerp as $onderwerp){
        echo '<div class="well well-sm">';
            if(file_exists('assets/images/profielfotos/'.$onderwerp['klant_id'])) { 
                echo '<img class="img-circle" src="/assets/images/profielfotos/'.$onderwerp['klant_id'].'/'.$onderwerp['foto'].'" />';
            } else {
                echo '<i style="font-size: 6em;" class="fas fa-user-circle"></i>';
            }
            echo '<b><a href="https://tom.lbmedia.nl/reactie"> '.$onderwerp['onderwerpnaam'].'</b></a>'; 
            echo ' - ' . $onderwerp['voornaam'] . ' ' . $onderwerp['achternaam'];
            echo '</div>';
        }
    ?>

【讨论】:

  • 哦 wauww .... 我没有包括那个...谢谢!有没有办法让图片变大?
  • 更新了代码。只需编辑style属性,修改font-size:
  • 谢谢老兄!我更新了问题,但仍然有一个小问题,如您所见?关于这个问题的任何提示/技巧/提示
【解决方案2】:

您可以将字体图标放在其他部分,它应该可以正常工作。 我认为您的 if 语句不允许您转到其他地方。只需像这样更新 if 条件:

$foto = '/assets/images/profielfotos/'.$onderwerp['klant_id'].'/'.$onderwerp['foto'].' 'images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if(file_exists($foto)) {
    echo '<img class="img-circle" src="'.$foto.'" />';
}
else{
 echo '<i class="fas fa-user-circle"></i>'
}

对于您的第二个问题: 您可以编写 CSS 类并在父级中使用它,在您的情况下它将是 &lt;div class="well align-children"&gt;

.align-children{
   line-height: 40px; /* depending on the size of your icon */
   display: inline-block;
   vertical-align: middle;
}

您可以在 html 文档中使用样式:

<style>
.align-children{
   line-height: 40px; /* depending on the size of your icon */
   display: inline-block;
   vertical-align: middle;
} 
</style>

【讨论】:

  • 是的,这对我有用!谢谢!你能再看看问题更新一下吗
  • 我猜你需要垂直对齐图标和文本。只需将 CSS 应用于“well”div 内的项目即可。
  • html文档中没有办法做到吗?
  • 是的!通过使用样式标签。
猜你喜欢
  • 1970-01-01
  • 2014-10-19
  • 1970-01-01
  • 2018-04-29
  • 2010-11-26
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多