没有图像时显示默认图像

时间:2014-10-08 07:35:41

标签: php html arrays

我希望在CMS中没有上传图像时显示默认图像。

到目前为止,我得到了这段代码:

<li><img alt="" class="img-responsive" src="
                        <? if($artikel[0]['images'] == ''){ ?>
                                images/defaultimg.jpg
                        <? }else{ ?>
                        cms/images/<?

                                $plaatje = $artikel[0]['images'];
                                $plaatje = explode('\/', $plaatje);
                                $plaatje = $plaatje[1];
                                $plaatje = explode('"', $plaatje);
                                $plaatje = $plaatje[0];
                                echo $plaatje;

                        }
                        ?>"></li>

但这不显示图像。我知道图像在那里,所以我认为输出的HTML代码有问题。

有人看到了吗?

由于

按要求输出代码:

<img alt="" class="img-responsive" src="
                                                                cms/images/">

我得到了正确的代码:

                <?php
                  $image = "images/defaultimg.jpg";

                  $pl = $artikel[0]['images'];
                  $pl = explode('\/', $pl);
                  $pl = $pl[1];
                  $pl = explode('"', $pl);
                  $pl = $pl[0];
                  $pl = explode('.', $pl);
                  $pl = $pl[1];

                  echo $pl;

                  if($pl == 'jpg'){ //Check if string is empty
                      $plaatje = $artikel[0]['images'];
                      $plaatje = explode('\/', $plaatje);
                      $plaatje = $plaatje[1];
                      $plaatje = explode('"', $plaatje);
                      $plaatje = $plaatje[0];
                      // If the string supplied in the if condition isn't empty i assume that there's an image
                      // so i prepend the imagefolder as source
                      $image = 'cms/images/'.$plaatje;
                  }

                  $html = '<li><img alt="" class="img-responsive" src="'.$image.'"></li>';

                  echo $html

                ?>

5 个答案:

答案 0 :(得分:2)

为什么不将if else逻辑与标记分开,而不是在源属性上混淆它。例如:

<?php

$image = '';
if($artikel[0]['images'] == '') {
    $image = 'images/defaultimg.jpg'; // wrap it with quotes
} else {
    $plaatje = $artikel[0]['images'];
    $plaatje = explode('\/', $plaatje);
    $plaatje = $plaatje[1];
    $plaatje = explode('"', $plaatje);
    $image = 'cms/images/' . $plaatje[0]; // append the filename with that particular path
}

?>

<li><img alt="" class="img-responsive" src="<?php echo $image; ?>"></li>

答案 1 :(得分:1)

试试这个

<li><img alt="" class="img-responsive" src="
                        <?php if(empty($artikel[0]['images'])){ 
                                echo "images/defaultimg.jpg";
                         }else{ 


                                $plaatje = $artikel[0]['images'];
                                $plaatje = explode('\/', $plaatje);
                                $plaatje = $plaatje[1];
                                $plaatje = explode('"', $plaatje);
                                $plaatje = $plaatje[0];
                                echo "cms/images/".$plaatje;

                        }
                        ?>"></li>

答案 2 :(得分:1)

AS @Ghost说,从视图中分离逻辑,另一个解决方案更干净:

<?php

    if (empty($artikel[0]['images']){
        $artikel[0]['images'] = 'images/defaultimg.jpg';
    }else{
        $artikel[0]['images'] = 'cms/images/' . basename($artikel[0]['images']);
    }

?>

<li><img alt="" class="img-responsive" src="<?php echo $artikel[0]['images']; ?>"></li>

答案 3 :(得分:0)

问题似乎出现在你的代码的布局中,因为你在输出中清楚地看到你有了换行符,可能还有更多未知的未知数。

更好地重构它,就像这样。

<?php
  $image = "images/defaultimg.jpg";
  if($artikel[0]['images'] != ''){ //Check if string is empty
      $plaatje = $artikel[0]['images']; //Do your magic here
      $plaatje = explode('\/', $plaatje);
      $plaatje = $plaatje[1];
      $plaatje = explode('"', $plaatje);
      $plaatje = $plaatje[0];
      // If the string supplied in the if condition isn't empty i assume that there's an image
      // so i prepend the imagefolder as source
      $image = 'cms/images/'.$plaatje;
  }

  $html = '<li><img alt="" class="img-responsive" src="$image"></li>';

  echo $html

?>

首先,这将提高您的代码的可读性,其次您可以毫不费力地看到您正在尝试做的事情。

这种方式你不必关心2个案例,你有一个案例要处理,其余的由默认变量处理。

但请注意,在你的$ artikel [0] ['images']中也可能会有一些错误,所以最后你会在你的imagesource中添加一个错误字符串,你不会这样做。< / p>

每当您向用户展示某些内容时,您应该检查该文件是否存在。再一次,你将那个未知的未知数放在那个变量中。

您的变量的分配将如下所示

if(file_exists('cms/images/'.$plaatje)){
   $image = 'cms/images/'.$plaatje;
}

这样可以防止出现上述问题,并且由于您只有一个退出点,因此在您之后处理该代码的人员将不胜感激。

答案 4 :(得分:0)

我得到了正确的代码。

                        <?php
                          $image = "images/defaultimg.jpg";

                          $pl = $artikel[0]['images'];
                          $pl = explode('\/', $pl);
                          $pl = $pl[1];
                          $pl = explode('"', $pl);
                          $pl = $pl[0];
                          $pl = explode('.', $pl);
                          $pl = $pl[1];

                          echo $pl;

                          if($pl == 'jpg'){ //Check if string is empty
                              $plaatje = $artikel[0]['images'];
                              $plaatje = explode('\/', $plaatje);
                              $plaatje = $plaatje[1];
                              $plaatje = explode('"', $plaatje);
                              $plaatje = $plaatje[0];
                              // If the string supplied in the if condition isn't empty i assume that there's an image
                              // so i prepend the imagefolder as source
                              $image = 'cms/images/'.$plaatje;
                          }

                          $html = '<li><img alt="" class="img-responsive" src="'.$image.'"></li>';

                          echo $html

                        ?>