Use php to set the column width depending on the result of the while

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 the while, a total of 3 results according to $quantidade_pg=3;

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

But if you only have 2 categories on the page, I would like the width to automatically change to 50% if you have only 1 category displayed at 100%.

Is there any way to get php to give the result of the width according to the number of categories displayed in the while result?

    
asked by anonymous 13.06.2017 / 14:48

1 answer

1

Do this by 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>  
    
13.06.2017 / 15:03