3

Display the default image when there is no image

 3 years ago
source link: https://www.codesd.com/item/display-the-default-image-when-there-is-no-image.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Display the default image when there is no image

advertisements

I want to display a default image when no image is uploaded in the CMS.

So far I got this code:

<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>

But this does not display the image. I know the image is there, so I think something is wrong with the outputted html code.

Anyone see it?

Thanks

Output code as requested:

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

I got the correct code:

                <?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

                ?>


Why not just separate the if else logic from your markup instead of cluttering it up on that source attribute. Example:

<?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>


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK