PHP condition for not displaying image when it does not exist

2

My problem is as follows. I have a table with several product images, but not all products I have 5 images (total BD columns). what I wanted was to "undo" the html line when the image does not exist. I tried this code

<?php
 if (!empty($imagem3)) {
 ?>
  <td class="coluna_foto"><img src="<?php echo $imagem3; ?>" width="75" height="75" ></td>
  <?php }else{
  return false;
} 
?>

The html line is always out of php and so it always stays with the link and therefore when there is no picture it always appears the icon to "say" that there is no photo ... Someone has a suggestion to solve

    
asked by anonymous 01.12.2015 / 17:43

2 answers

3

Put your image in the echo to write the page, so keep everything on the server side.

<?php
 if (!empty($imagem3)) {
    echo("<td><img src='". $imagem3 ."'></td>")
 }
?>

Does this solve your problem?

    
01.12.2015 / 17:52
2

I'm not sure if in your case the image3 is returning null or empty, but try this code.

so if you want to see a default image if you do not have it in the database.

<?php 
     $link_da_imagem = ($imagem3 == "fotografias/" ? "/caminho/para/imagem/vazia.jpg" : $imagem3); 
?>
<img src="<?php echo $link_da_imagem; ?>" width="75" height="75">

so if you do not want anything to appear:

<?php 
     if ($imagem3 != "fotografias/") { 
?>
        <img src="<?php echo $imagem3; ?>" width="75" height="75">
<?php 
     } 
?>
    
01.12.2015 / 17:51