Split query result into columns

1

I have a select that brings items with prices, how do I split the result into two columns:  Examples:

Ihopetoleaveitasfollows:

Here'smycode:

<html><head><metaname="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 for every 10 products to create a new column.

    
asked by anonymous 21.08.2018 / 22:20

1 answer

1

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

<body nload="toggleFullScreen()">

It also has a </div> closure remaining after while .

Furthermore, 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 even with $numrows%2 . If it is not even, the first column will have 1 more line.

I created two variables $media (result of division by 2) and $conta (will count the written lines and compare with $media ). When one variable is equal to another, the previous column is closed and a new one is 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>
    
22.08.2018 / 00:13