Use a column as a header

Asked

Viewed 72 times

0

I have a table in mysql of products, and need in select or php turns a column into header, example:

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: Need to display your screen on something like card’s separate with headers, example:

inserir a descrição da imagem aqui

1 answer

1


Right after you select (something like mysqli_query(....)), you can mount a two-dimensional vector from the resultset returned by mysqli_query(). With this one can obtain a vector more or less 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>';
}

Browser other questions tagged

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