Split query result into columns

Asked

Viewed 252 times

1

I have a select that brings items with prices, as I do to divide the result into two columns: Examples: inserir a descrição da imagem aqui

I hope to leave it this way: inserir a descrição da imagem aqui

Follow my code:

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TABELA OFERTAS</title>
    <link rel="stylesheet" href="dist/css/bootstrap.min.css">
    <script src="dist/js/jquery.min.js"></script>
    <script src="dist/js/bootstrap.min.js"></script>
    <style type="text/css">
    body {
        background-image: url("img/fundo.jpg");
        width: 100%;
        height: auto;
    }
</style>    
</head>
<body nload="toggleFullScreen()">
    <div class="container">
        <br>
        <div class="panel panel-default">
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-4">
                        <img src="img/logo.png" style="width: 150px;">
                    </div>
                    <div class="col-md-8">
                        <h3>OFERTAS</h3>
                    </div>
                </div>


            </div>
        </div>
        <br>
        <?php
        include 'data/conexao.php';
        $listaProdutos = "SELECT * FROM prod_imp";
        $result = mysqli_query($conn, $listaProdutos);
        while($row = mysqli_fetch_assoc($result)) { ?>

            <div class="row">
                <div class="col-md-6">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <?php echo "<h3>". $row["prod_imp_nome"] ."</h3>"; ?>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <?php echo "<h3> R$" . str_replace('.', ',', $row["prod_imp_preco"] ). "KG </h3>"; ?>
                        </div>
                    </div>                              
                </div>
            </div>
        <?php } ?>
    </div>  
</div>
</body>
</html>

The idea would be, every 10 products create a new column.

  • @No, it’ll always be two columns

  • @Sam knows how to implement this in the code ?

  • Perhaps it would be better to divide by 2 columns when it is above 1, no?

  • @sam can be too

  • @That’s right, thank you, post your reply to be able to dial please

1 answer

1


Just noting an error in the syntax in onload of the body:

<body nload="toggleFullScreen()">

Also has a closure </div> remaining after the while.


In addition, to divide the result into two columns col-md-6 you can get the number of results with $numrows = mysqli_num_rows($result); and divide by 2. Check that the result is matched with $numrows%2. If not even, the first column will have 1 more row.

I created two variables $media (result of division by 2) and $conta (will count written lines and compare with $media). When one variable is equal to another, the previous column will be closed and a new one created.

<div class="container">
   <br>
   <div class="panel panel-default">
      <div class="panel-body">
         <div class="row">
            <div class="col-md-4">
               <img src="img/logo.png" style="width: 150px;">
            </div>
            <div class="col-md-8">
               <h3>OFERTAS</h3>
            </div>
         </div>
      </div>
   </div>
   <br>
   <div class="row">
      <div class="col-md-6">
   <?php
   include 'data/conexao.php';

   $listaProdutos = "SELECT * FROM prod_imp";
   $result = mysqli_query($conn, $listaProdutos);
   $numrows = mysqli_num_rows($result);
   $media = floor($numrows/2);
   $maxrow = $media;
   $conta = 1;
   if($numrows%2 != 0) $maxrow = $media+1;
   while($row = mysqli_fetch_assoc($result)) {
   ?>
         <div class="panel panel-default">
            <div class="panel-body">
               <?php echo "<h3>". $row["prod_imp_nome"] ." R$ ". str_replace('.', ',', $row["prod_imp_preco"] ). "KG</h3>"; ?>
            </div>
         </div>
   <?php
      if($conta == $maxrow){
   ?>
      </div>
      <div class="col-md-6">
   <?
      }
      $conta++;
   }
   ?>
      </div>
   </div>
</div>

Browser other questions tagged

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