If condition not to show images that do not exist PHP

Asked

Viewed 113 times

-1

<fieldset>
                    <legend>FOTOS</legend>  
                        <div class="form-group">

                            <?php
                                if ($linha['foto1_bci'] && $linha['foto2_bci'] && $linha['foto3_bci'] && $linha['foto4_bci'] && $linha['foto5_bci']) { ?>
                                    <label for="foto1_bci">Foto 1</label>
                                    <img class="img-responsive" src="fotos/<?=$linha['foto1_bci']?>" width="500px" height="200px"></img>

                                    <label for="foto2_bci">Foto 2</label>
                                    <img class="img-responsive" src="fotos/<?=$linha['foto2_bci']?>" width="500px" height="200px"></img>

                                    <label for="foto3_bci">Foto 3</label>
                                    <img class="img-responsive" src="fotos/<?=$linha['foto3_bci']?>" width="500px" height="200px"></img>

                                    <label for="foto4_bci">Foto 4</label>
                                    <img class="img-responsive" src="fotos/<?=$linha['foto4_bci']?>" width="500px" height="200px"></img>

                                    <label for="foto5_bci">Foto 5</label>
                                    <img class="img-responsive" src="fotos/<?=$linha['foto5_bci']?>" width="500px" height="200px"></img>
                            <?php } ?>
                        </div>
                </fieldset>

inserir a descrição da imagem aqui

  • What is the doubt?

  • How can I show only the images that exist? Because I only uploaded image 1 but it shows an icon as if it did not display and the name Foto2 Foto3 Foto4, I would like it not to be displayed if the image did not exist.

3 answers

1

I commented the code for better understanding, you better use a foreach as it is better to avoid rework if you need to put an extra photo or remove, for is something manual where you arrow the positions, with foreach you will run all independent positions of how many have.

<?php
// Iniciando contador
$i = 0;
// Rodando todas as posições
foreach ($linha as $key => $foto) {
    // Verificando se a foto é nula ou vazia
    if($foto != '' && $foto != null) { ?>
    <!-- Montando a label e a imagem -->
    <label for=<?php 'foto'.$i.'_bci' ?>>Foto <?php $i ?></label>
    <img class="img-responsive" src="fotos/<?php echo $foto ?>"></img>
<?php
    }
} 
?>

1

Emmanuel, you can improve by performing a for and checking if the item is different from nothing.

<?php
for($i=1; $i<=5; $i++) {
   if(isset($linha['foto'.$i.'bci']) && $linha['foto'.$i.'bci'] != '') { ?>
       <label for="<?='foto'.$i.'bci'?>">Foto <?=$i?></label>
       <img class="img-responsive" src="fotos/<?=$linha['foto'.$i.'bci']?>" width="500px" height="200px"></img>
   <?php
   }
}

0

Hello @Emmanuel,

Do you have a field for every photo in the database that? So you save the name of the image in each of them.

  • An alternative would be to save the name of the images in a single field, and perform the foreach for reading.

  • If you want to keep in separate fields as you are currently doing, you can do the following to show only the existing images by checking the fields of the filled and empty bd:

    $sql = "SELECT imagem1,imagem2,imagem3 FROM table";
    

    $rs_sql = mysqli_query($conex,$sql);

    while ($row = mysqli_fetch_array($rs_sql)) {
           $imagem1 = $row['imagem1'];
           $imagem2 = $row['imagem2'];
           $imagem3 = $row['imagem3'];
    }
    
    $imagens = "$imagem1,$imagem2,$imagem3";
    $imagens = explode(",",$imagens);
    
    foreach($imagens as $imgs){             
    
                if($imgs!=""){
    
                    $imgs_src []= "         
                        <img src='$imgs' width='150px' height='150px' />        
                    ";  
    
                }//end if   
    
        }//end foreach
    
    
    
        echo join($imgs_src);
    

Browser other questions tagged

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