Group records, but show repeaters in a row

Asked

Viewed 51 times

0

I have a table where I take all orders made on the day, but show all in a table, so far so good, but the customer needs to be grouped by those who asked, I will give an example,

On my table I have

ID_PEDIDO | ID_USER_PEDIDO | PEDIDO

1 | 20 | 524

5 | 52 | 258

2 | 35 | 525

6 | 52 | 253

3 | 20 | 658

4 | 20 | 358

The result appears to me as query and shows me all requests in order of request SELECT * FROM orders ORDER BY ID_PEDIDO ASC

If I put a GROUP BY ID_USER_PEDIDO will group the orders according to the user and show mo only 1

I need to be shown all of them grouped in the same table row

 <table id="datatable1" class="table display responsive ">
              <thead>
                <tr>
                  <th class="">ID</th>
                  <th class="">ID do usuario</th>
                  <th class="">Pedido</th>               
                </tr>
              </thead>
              <tbody>
                  
                  <?php
                    $sql = "SELECT * FROM pedidos  ORDER BY ID_PEDIDO ASC";
                    $result = $conn->query($sql);                    
                      while($row = $result->fetch_assoc()) {                        
                    
                  ?>
                <tr>
                  <td><?php echo $row["ID_PEDIDO"];?></td>
                  <td><?php echo $row["ID_USER_PEDIDO"];?></td>
                  <td><?php echo $row["PEDIDO"];?></td>
                </tr>
                <?php
               
                      }
                ?>
              </tbody>
            </table>

Have some way to appear as per the image below?

inserir a descrição da imagem aqui

  • Picture below?? see picture no picture

  • Sorry, the image didn’t go, I just entered

1 answer

1


<?php
    $host = "localhost";
    $username = "USUARIO";
    $password = "SENHA";
    $db = "NOME_DB";
                
                
    // Criando connexão
    $conn = new mysqli($host, $username, $password, $db);
    // Checando conexão
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
                    
        echo '<table id="datatable1" class="table display responsive" border="1">
          <thead>
            <tr><tr><th class="">ID</th><th class="">ID do usuario</th>
              <th class="">Pedido</th></tr>';
              
               /* ######## retorna um valor para cada grupo de registro ID_USER_PEDIDO ######### */
                $sql = "SELECT * FROM pedidos GROUP BY ID_USER_PEDIDO ORDER BY ID_USER_PEDIDO ASC";
                $result = $conn->query($sql);                    
                  while($row = $result->fetch_assoc()) {   
                                    
                    $meio = $row["ID_USER_PEDIDO"];
              /* ############################################################################### */

                    
              /* #### extrai apenas os registros que atendem a condição ID_USER_PEDIDO = $meio ###### */
                    $sql2 = "SELECT * FROM pedidos WHERE ID_USER_PEDIDO = $meio ORDER BY ID_PEDIDO ASC";
                    $result2 = $conn->query($sql2); 
                    
                    while($row2 = $result2->fetch_assoc()) { 
                      $ini .= $row2["ID_PEDIDO"]."<br>";                          
                      $fim .= $row2["PEDIDO"]."<br>";
                    }
              /* ############################################################################### */
                    
                    /* ########### Monta e concatena as linhas da tabela ##################### */
                      $linhas .= ("<tr><td>".$ini."</td><td>".$meio."</td><td>".$fim."</td></tr>");
                    /* ######################################################################### */
                    
                    /* apagando as variaveis */
                    unset($ini);
                    unset($fim);
                    
                    
                    
                } 
                
        /* ##### imprimindo na tela o resultado final ##### */                               
        echo '</thead><tbody>'.$linhas.'</tbody></table>';

           

?>
  • Valew . Thanks for your help,

Browser other questions tagged

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