Use php to set column width depending on while result

Asked

Viewed 121 times

0

I have the following code

$result_categorias = "SELECT * FROM categorias ORDER BY ordem ASC";
$resultado_categorias = mysqli_query($conn, $result_categorias);
$total_categorias = mysqli_num_rows($resultado_categorias);
$quantidade_pg = 3;
<div class="row">
   <?php while($row_categorias = mysqli_fetch_assoc($resultado_categorias)){?>
   <div class="col-md-6">
      <article class="mr-categorias-single">
         <div class="mr-categorias-img">
            <img src="imagens/categorias/<?php echo $row_categorias['imagem']; ?>" width="360" alt="">
            <div class="mr-categorias-single-content">
               <a href="produtos.php?&id=<?php echo $row_categorias['id']; ?>">
                  <h3><?php echo $row_categorias["nome"]; ?></h3>
                  <span><?php echo $row_categorias["resumo"];  ?></span>
               </a>
            </div>
         </div>
      </article>
   </div>
   <?php } ?>
</div>

Pull the categories and display through while, a total of 3 results according to the $quantidade_pg=3;

The beginning of the while I have a <div class="col-md-6"> which is in the 33% width css file to split the total into 3 columns.

But if somehow only have 2 categories on the page, would like the width automatically changes to 50% if it has only 1 category displayed in 100%.

Is there any way to make php when giving the width result according to the number of categories displayed in the while result?

  • 1

    you want to modify the class for the col-md-* or you want to set the width of div?

  • 1

    it is better you make your layout adapt to the content in some way than use backend to manipulate layout. golden tip.

  • Thanks @Leandro - Good tip.

1 answer

1


Do it by php using a control variable, and then just print it in the container:

Example:

<?php
$result_categorias = "SELECT * FROM categorias ORDER BY ordem ASC";
$resultado_categorias = mysqli_query($conn, $result_categorias);
$total_categorias = mysqli_num_rows($resultado_categorias);
$quantidade_pg = 3;

$class="col-md-6";
if($total_categorias == 1) {
     $class="col-md-12";
}
?>
<div class="row">
            <?php while($row_categorias = mysqli_fetch_assoc($resultado_categorias)){?>
            <div class="<?php echo $class; ?>">
              <article class="mr-categorias-single">
                <div class="mr-categorias-img">
                                <img src="imagens/categorias/<?php echo $row_categorias['imagem']; ?>" width="360" alt="">
               <div class="mr-categorias-single-content">                      
                 <a href="produtos.php?&id=<?php echo $row_categorias['id']; ?>"><h3><?php echo $row_categorias["nome"]; ?></h3>
                     <span><?php echo $row_categorias["resumo"];  ?></span></a>
                </div>                    
                </div>         
                  </article>
                </div>  
               <?php } ?>
            </div>  
  • 1

    Done! @Hiago Souza, was the path I needed. Thanks worked.

Browser other questions tagged

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