Use a column as a header

0

I have a table in mysql of products, and I need in select or php to transform a column into header, eg:

cod    produto    valor    pagina
123    caderno     1,00    pagina 1
456    lápis       1,00    pagina 1
789    borracha    1,00    pagina 1
1122   régua       1,00    pagina 2
1123   cola        1,00    pagina 2
1122   caneta      1,00    pagina 3

Expected result:

cod    produto    valor 

pagina 1
123    caderno     1,00    
456    lápis       1,00  
789    borracha    1,00  

pagina 2
1122   régua       1,00    
1123   cola        1,00  

pagina 3
1122   caneta      1,00   

Reason: I need to display the screen in something like separate card's with the headers, eg:

    
asked by anonymous 18.12.2017 / 22:55

1 answer

0

Shortly after doing select (something like mysqli_query(....) ), you can assemble a two-dimensional array from the resultset returned by mysqli_query() . With this you can get a vector something like this:

array (size=3)
  'pagina 1' => 
    array (size=3)
      0 => 
        array (size=4)
          'cod' => string '123' (length=3)
          'produto' => string 'caderno' (length=7)
          'valor' => string '1' (length=1)
          'pagina' => string 'pagina 1' (length=8)
      1 => 
        array (size=4)
          'cod' => string '456' (length=3)
          'produto' => string 'l�pis' (length=5)
          'valor' => string '1' (length=1)
          'pagina' => string 'pagina 1' (length=8)
      2 => 
        array (size=4)
          'cod' => string '789' (length=3)
          'produto' => string 'borracha' (length=8)
          'valor' => string '1' (length=1)
          'pagina' => string 'pagina 1' (length=8)
  'pagina 2' => 
    array (size=2)
      0 => 
        array (size=4)
          'cod' => string '1122' (length=4)
          'produto' => string 'r�gua' (length=5)
          'valor' => string '1' (length=1)
          'pagina' => string 'pagina 2' (length=8)
      1 => 
        array (size=4)
          'cod' => string '1123' (length=4)
          'produto' => string 'cola' (length=4)
          'valor' => string '1' (length=1)
          'pagina' => string 'pagina 2' (length=8)
  'pagina 3' => 
    array (size=1)
      0 => 
        array (size=4)
          'cod' => string '1124' (length=4)
          'produto' => string 'caneta' (length=6)
          'valor' => string '1' (length=1)
          'pagina' => string 'pagina 3' (length=8)

The following is a code implementation to do this (with a small demonstration of how to display):

<style>
div{
    display: inline-block;
    border: 2px solid red;
    margin-right: 10px;
}

</style>
<?php
//....
$resultado = mysqli_query($conexao, 'select * from produtos');

$paginas = [];

while($linha = mysqli_fetch_assoc($resultado)){
    $paginas[$linha['pagina']][] = $linha;
}

var_dump($paginas);

foreach($paginas as $pagina){
    echo '<div>' . $pagina[0]['pagina'] . '<br>';
    echo '<table><tbody>';
    foreach($pagina as $linha){
        echo '<tr><td>' . $linha['cod'] .'</td><td>' . $linha['produto'] . '</td><td>' 
        . $linha['valor'] . '</td><td>' . $linha['pagina'] . '</td></tr>'; 

    }
    echo '</tbody></table></div>';
}
    
19.12.2017 / 00:04