php filter data within the while

-2

I'm having trouble filtering within a while the data I get from a database. I am writing HTML + PHP generate the layout and to get the sqlite database data.

Table: entries select * from ORDER BY data;

numero  nome     data
4       Manuel   2018-11-17
6       Rui      2018-11-17
6       Rui      2018-11-17
2       Raul     2018-11-17
2       Raul     2018-11-17
2       Raul     2018-11-17
3       Pedro    2018-11-17
3       Pedro    2018-11-17
2       Raul     2018-11-18
4       Manuel   2018-11-18
6       Rui      2018-11-19
6       Rui      2018-11-19
2       Raul     2018-11-19
2       Raul     2018-11-19

My difficulty is to get HTML and PHP to filter and group the data in this way but with some conditions, if the name and date are the same I create and together in a select menu, if different on the date or name I create a new select menu.
example:

    <!DOCTYPE html> 
    <html>
    <body>

    <select>
      <option value="x">Manuel 2018-11-17</option>
    </select>
    <select>
      <option value="x">Rui 2018-11-17</option>
      <option value="x">Rui 2018-11-17</option>
    </select>
    <select>
      <option value="x">Raul 2018-11-17</option>
      <option value="x">Raul 2018-11-17</option>
      <option value="x">Raul 2018-11-17</option>
    </select>
...
 <select>
      <option value="x">Rui 2018-11-19</option>
      <option value="x">Rui 2018-11-19</option>
    </select>
...    
    </body>
    </html>
    
asked by anonymous 17.11.2018 / 21:14

2 answers

0

Are you sure you want to have two option equal within select ? It does not make sense. If so, I made an example below that fits what you need.

Basically, the list of records returned from the database was mapped and the html elements created for them.

<?php

$registros = array(
    (object) ['numero' => 4, 'nome' => 'Manuel', 'data' => '2018-11-17'],
    (object) ['numero' => 6, 'nome' => 'Rui', 'data' => '2018-11-17'],
    (object) ['numero' => 6, 'nome' => 'Rui', 'data' => '2018-11-17'],
    (object) ['numero' => 2, 'nome' => 'Raul', 'data' => '2018-11-17'],
    (object) ['numero' => 2, 'nome' => 'Raul', 'data' => '2018-11-17'],
    (object) ['numero' => 2, 'nome' => 'Raul', 'data' => '2018-11-17'],
    (object) ['numero' => 3, 'nome' => 'Pedro', 'data' => '2018-11-17'],
    (object) ['numero' => 3, 'nome' => 'Pedro', 'data' => '2018-11-17'],
    (object) ['numero' => 2, 'nome' => 'Raul', 'data' => '2018-11-18'],
    (object) ['numero' => 2, 'nome' => 'Raul', 'data' => '2018-11-18'],
    (object) ['numero' => 4, 'nome' => 'Manuel', 'data' => '2018-11-18'],
    (object) ['numero' => 6, 'nome' => 'Rui', 'data' => '2018-11-19'],
    (object) ['numero' => 6, 'nome' => 'Rui', 'data' => '2018-11-19'],
    (object) ['numero' => 2, 'nome' => 'Raul', 'data' => '2018-11-19'],
    (object) ['numero' => 2, 'nome' => 'Raul', 'data' => '2018-11-19']
);

function mapeiaRegistros($registros)
{
    // Como deve ser identificado pelo nome e data é feito o mapeamento desses dados ao invés do objeto
    // Observação: caso o usuário precise do numero basta apenas colocar o $r->numero na frente do $r->nome e concatenar
    $mapear = function($r) {return $r->nome . ' ' . $r->data;};
    $registros = array_count_values((array_map($mapear, $registros)));
    return $registros;
}

$registros = mapeiaRegistros($registros);

?>

 <!DOCTYPE html>
 <html dir="ltr" lang="pt-BR"> 
     <head>
     <meta charset="UTF-8" />
     </head>
     <body>
         <div id="dados"></div>
     </body>
     <script>

        var div = document.createElement('div');

        <?php
             foreach($registros as $nomeData => $contagem)
             {
        ?>      
                var select = document.createElement("select");
        <?php
                for($i = 0; $i < $contagem; $i++)
                {
        ?>
                    var option = document.createElement("option");
                    option.id = '<?=$i?>';
                    option.text = '<?=$nomeData?>';
                    option.value = '<?=$nomeData?>';
                    select.appendChild(option);
        <?php   
                }
        ?>
                div.appendChild(select);
                div.appendChild(document.createElement("br"));
        <?php    
             }
         ?>

         document.getElementById("dados").innerHTML = div.innerHTML;

     </script>
 </html>

Image of the result:

    
17.11.2018 / 23:10
0

I believe that what you want can be done with simple comparisons and a for loop, self-explanatory logic, but it does not make much sense because all option has the same internal HTML, the?

select * from entradas ORDER BY data, nome

$select = '';
for ($i = 0; $i < count($entradas); $i++) {

    $select .= '<select>';
    for ($j = $i; $entradas[$i]['nome'] == ($entradas[$j]['nome'] ?? ''); $j++) {
        $select .= "<option value='{$entradas[$j]['numero']}'>{$entradas[$j]['nome']} {$entradas[$j]['data']}</option>";
    }
    $select .= '</select>';

    $i = $j-1;
}

echo $select;
    
17.11.2018 / 22:26