if not condition to show images that does not exist PHP

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

    
asked by anonymous 29.06.2018 / 16:31

3 answers

1

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

<?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
   }
}
    
29.06.2018 / 17:06
1

I commented the code for a better understanding, you better use a foreach for being better at avoiding rework in case you need to put a photo more or withdraw, the for is something manual where you arrow the positions, with foreach you go run as many independent positions as you 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
    }
} 
?>
    
29.06.2018 / 18:46
0

Hello @ Emmanuel,

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

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

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

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

    $ rs_sql = mysqli_query ($ connect, $ 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);
    
29.06.2018 / 19:32