How to place an image inside echo and not show if it is empty

Asked

Viewed 340 times

0

I made the code to show only if you have image, but it also shows the body of empty, it says in the source code of the page that my PHP is a comment as shown in the image below.

inserir a descrição da imagem aqui

And the code is this:

inserir a descrição da imagem aqui

How do I not show the empty pictures the way you are here?

echo '

                  <php

                  $seleciona = mysqli_query($conexao, "SELECT * FROM postagem where status=1 ORDER BY id desc");
                  while($campo=mysqli_fetch_array($seleciona)){
                    $nome_imagem = $campo["nome_imagem"];                    
                    ?>
                    <center>
                    <div id="panel" align="left">
                    <label class="titulo">&nbsp;&nbsp;'. $result1[$i][1] . '</label><br>
                    <p class="descricao">'. $result1[$i][2] . '</p><br>

                    <?php if ($nome_imagem != null){?><p><img src="foto/'. $result1[$i][3] . '" class="foto"></p><?php } ?>

                    <span class="glyphicon glyphicon-user" aria-hidden="true"></span>&nbsp;&nbsp;Postado por: '. $result1[$i][6] . ' em '. $result1[$i][4] . ' às '. $result1[$i][5] . '</td>&nbsp;&nbsp;

                    <a href="0_excluir_postagem.php?editaid='. $result1[$i][0] . '"><span class="glyphicon glyphicon-trash"></span></a>&nbsp;&nbsp;     
                    </div></center>  

                    <?php }?> 
                    </div>' ; 

                  }
  • The syntax is wrong, remove this echo ' at the beginning, for the image, use the function is_file()

  • But it’s inside a FOR: for ($i = 0; $i < Count($result1); $i++) { #Coding }

  • Tries $nome_imagem != ""

2 answers

2

Why you do not create a verification method if the image is valid:

<?php 
function checkImage($img = null, $nome = null)
{
   if ($img != null && $nome != null) {
       return true;
   }
   return false;
   
} ?>


<?php if (checkImage($result1[$i][3],$nome_imagem)):?>
    <p><img src="foto/'. $result1[$i][3] . '" class="foto"></p>
<?php endif; ?>

0

Alter:

<?php if ($nome_imagem != null){?><p><img src="foto/'. $result1[$i][3] . '" class="foto"></p><?php } ?>

To:

<?php if (!empty($nome_imagem)) { ?><p><img src="foto/'. $result1[$i][3] . '" class="foto"></p><?php } ?>

I don’t understand the logic of testing a variable ($nome_imagem) and display another ($result1[$i][3]). But it’s okay. It’s not the point of the question, let alone the answer.

Back to the question, about the empty():

Returna FALSE if the tested variable exists and is not empty. Otherwise, returns TRUE.

empty() considers the following cases as emptiness:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a floating point)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a declared variable but no value)

Reference: PHP - Empty

Compare the variable with null would ignore all these other mentioned cases. (if ($nome_imagem != null){ //...).

  • The same result, it shows the ones that have photos and also the space of the "IMAGES" that do not have.

  • @Alisonwalker - "and also the space of the "IMAGES" that do not have". That means you’re storing something where it should not be stored in the database. In this case, you must improve the way you store this data in your db or create a function that understand certain data (this something) as pictureless... If your db follows a pattern for when there is no photo, it is easy to create this function. Just post (editing the question) this something which is stored in your db when there is no photo.

Browser other questions tagged

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