Display first image of a dynamic table in php

Asked

Viewed 54 times

1

I would like to know how to display the first image of my Mysql table?

I used a dynamic input file, so I can have several names saved in a single table that in this case is the image. However, I would like to know how I display only the first?

The names in the comic are separated by ;

<div class="lista-prod col-lg-12 ">
<?php 
   $verificaQuery = mysqli_query($conexao,"select * from produtos");
   $verificaRow = mysqli_num_rows($verificaQuery);

       if ($verificaCount = 0) {

       } else {
           $contProd=0;
           while ($contSql = mysqli_fetch_array($verificaQuery)) {
            $contProd++;
       ?>
<div class="row mt-5">
<div class="col-lg-4">
   <img src="../imagens/produtos/<?php echo $contSql['imagem'];  ?>" style="width:100%;">
</div>

If I register only one image it appears normally, but if I register more, it disappears. To summarize, I have a products page, the first image is the one that gets sample if the person opens the product, displays the other photos beside, as I control it?

  • 1

    <?php echo explode(';', $contSql['imagem'])[0]; ?>

  • Perfect, it was just that, thank you Thiago

1 answer

2

Even with the reply of the comment, I will leave just below another example of code with greater organization:

<div class="lista-prod col-lg-12 ">
    <?php 
        $verificaQuery = mysqli_query($conexao,"select * from produtos");
        $verificaRow = mysqli_num_rows($verificaQuery);
        if ($verificaRow != 0) {    
           while ($contSql = mysqli_fetch_array($verificaQuery)) {
              $arrayDeImagens = explode(';', $contSql['imagem']);
              $primeiraImagem = $arrayDeImagens[0];
              echo "<div class='row mt-5'>
                       <div class='col-lg-4'>
                           <img src='../imagens/produtos/$primeiraImagem' style='width:100%;'>
                       </div>
                    </div>"; 
           }
        }            
    ?>
</div>

The code will print the HTML with the src tag img being the first image of each row returned

Browser other questions tagged

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