PHP condition not to display image when it does not exist

Asked

Viewed 1,249 times

2

My problem is the following.I have a table with various product images, but not in all products I have 5images (total BD columns). what intended 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;
} 
?>

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

  • What is your default image column value in the BD?

  • Is the image inside the BD, or are they files and the BD points to them? And what is the false Return? It wouldn’t just be taking the part of Else?

  • @Bacco, what I have inside the comic is the name of the image. this faulty "$imagem3" has the way and this working.

  • @Tiagop. C the default value is "NULL"

  • the problem is that it always writes the tag img and therefore always appears that little cross indicating that there is no picture!

  • @Joaquimribeiro the answers below should probably solve the problem, but if it is not enough, find a way to the name of the image to be written on the page also as a test, it may be that some value is coming improper.

  • First, you don’t need to put the logical node when it returns empty (else { /* faz nada */}. Then give a var_dump variable and see what appears when it is "empty". It may be NULL, for example, then you would have to add && !is_null ($imagem3) === FALSE

Show 2 more comments

3 answers

3

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

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

That solves your problem?

  • 2

    Just for comment, everything is server-side, and it’s the same as "in and out" of HTML (PHP gives almost an implicit echo when you use ?>). Now, keeping a cleaner pattern is better, it is. Only if you write 30 lines of html, echo gets even uglier. PS: Next week I’ll have a coffee with Markinho, and I’ll take advantage to to ask for their surrender back. Anyway, just taking false should help the author, so +1.

  • 1

    Assemble this list by ajax, or return a JS array and mount it dynamically, with cool effects for the user to be happy. : D

  • did not work. the link continues there and deformed all the rest of the page!

  • 1

    @Joaquimribeiro And you’re linking to where?

  • in the database as the rest. and I have the variables $imagem1,$imagem2 and $imagem3. last left empty the field in the database in case you don’t have 3 images for each article

  • @Joaquimribeiro Yes, but in broken HTML the image link appears as? (see in source code)

  • <img width="75" height="75" src="photographs/"> , this is what appears @Bacco

  • @Joaquimribeiro And where does this "photographs" come from? It is probably him who is in the wrong place of his code. It should be <img src="fotografias/<?php echo $imagem3; ?> but these photographs must be being put in the wrong place of php and will never give Empty.

  • alias, in this @Rboschini solution gives the following error: Parse error: syntax error, Unexpected '}', expecting ',' or ';' in C: wamp www dtcr consuta_teste.php on line 68

  • @Joaquimribeiro this is just the lack of ";" after echo. But look what I said, because that’s probably the mistake. Since you didn’t post the code that picks up the comic book stuff, you can’t be sure.

Show 5 more comments

2

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

so if you want a default image to appear if you don’t 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 don’t want anything to come up:

<?php 
     if ($imagem3 != "fotografias/") { 
?>
        <img src="<?php echo $imagem3; ?>" width="75" height="75">
<?php 
     } 
?>
  • 1

    I think the placeholder as an alternative. If you format the code a little better, I think it’s easier for people to understand and vote.

  • ident for better visualization.

  • this was the way I tried. but since the img tag is out of php, it automatically shows a crux indicating that the image is missing. if I can’t find a better option, I’ll create an empty photo, which was the alternative solution I had already thought about! Thank you!

  • In the second alternative I put, the img tag is outside the php tag, but it is within a conditional, making the image not appear. it is worth trying.

  • I tried, of course.. but still there the raw! I think I choose the first option, it is not perfect but for now it is the best that I can. Thanks again

  • @Joaquimribeiro then it is not entering the condition of verification if it is null, you can substitute for the blank check Empty($imagem3) or check what is coming from the database using print_r($imagem3), so I understood the image is in the database, the image may also be corrupted there in the database.

  • the image does not exist @Hoppy! what I intend is that when there is no image in the database, that the img tag is not started to not have that little cross to appear on the page

  • Does this attempt: <?php if (!is_null($imagem3) && !Empty($imagem3) && isset($imagem3)) { ? > <img src="<? php echo $imagem3; ? >" width="75" height="75"> <? php } ?>

  • @Hoppy, I did a var_dump and as I said the result is: string 'photographs/' (length=12) in those articles where the product exists, it has string 'photographs/0.51942700 1448971409.jpg' (length=37)

  • I edited my answer, take a look.

  • the cross continues to appear. ... ta dificil!

  • @Hoppy, can you tell me why that answer code doesn’t work? <? php if (Empty($imagem3)) { $imagem3="photographs/empty.jpg"; } ?>

  • because $imagem3 is not empty, according to what you posted in the dump. you are filling the $imagem3 variable with "image/" . .... even with the database with the empty column, or else you are already writing in the database with "image/"

Show 8 more comments

-2

<?php
$imagem3 = "caminho/da/imagem/img.png";

 if (file_exists($imagem3)) {
    echo("<img src='".$imagem3."'>");
 }

?>

this code will work!

  • 2

    why does this code work? could you explain?

  • Describe the answer better, it’s too shallow

Browser other questions tagged

You are not signed in. Login or sign up in order to post.