php filter data inside while

Asked

Viewed 63 times

-1

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

Table: entries select * from ORDER BY data entries;

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 manage with HTML and PHP to filter and group the data in this way but with some conditions, if the name and date are the same create and together in a select menu, if it is different in date or name 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>
  • 1

    And what’s the use of that, it seems kind of pointless.

  • I am sorry if it has become meaningless in any way, but it was the quick way I could explain what I intended to do. Actually I am not using select menu but generating several listview with Forms using JQM so that I can process the information contained there. image

  • Well, then, ask the question "with reality" because otherwise you run the risk of the question being closed and you get a negative vote.

2 answers

0

I believe that whatever you want can be done with simple comparisons and a bow for, self-explanatory logic, but it doesn’t make much sense, because all the option has the same internal HTML, as you will differentiate them?

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;

0


Are you sure you want to have two option equal within the select? It doesn’t make much sense. If yes, 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>

Result image:

inserir a descrição da imagem aqui

  • But how to apply build the $records array in the while I have? while($row = $ret->fetchArray(SQLITE3_ASSOC) ) {&#xA;&#xA; }

  • Depending on what you are using you can receive the data as an object and then insert it into the variable records and therefore remove that while as I presented. For example, if you are using PDO you can use the $result = $statement->fetchAll(PDO::FETCH_ASSOC); and then treat according to the $record hold variable.

Browser other questions tagged

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