How to group array and mount select

Asked

Viewed 70 times

-1

How can I group array results and mount select.

Database

inserir a descrição da imagem aqui

PHP

<?php

 $rsPA = $mysqli->query("SELECT * FROM provas_agendadas WHERE status = 'A' ");

 foreach ($rsPA as $key => $rsRowPA){
   $dis1[] = explode("," , $rsRowPA['disciplinas']);
 }

 echo '<pre>';print_r($dis1);echo '</pre>';

?>

Upshot:

Array
(
    [0] => Array
        (
            [0] => EJA-1
        )

    [1] => Array
        (
            [0] => EJA-1
            [1] => EJA-5
            [2] => TTI-1
        )

)

Explaining: From the array result, I will only pick up the number after the -, Ex: EJA-1, I just need the 1, which is the discipline table ID.

Final result I would like

inserir a descrição da imagem aqui

I thank everyone who can help me.

  • search on array_search, it should solve your problem: https://www.php.net/manual/en/function.array-search.php

  • The name of the discipline will always follow exactly this model <string>-<id>? if so you can combine the functions substr and strrpos to extract the id, like this $disciplina = 'eja-5'; $id = substr($disciplina, strrpos($disciplina, '-') + 1);

  • @Diegomartins yes always this model <string><hifen><id>

  • @Diegomartins and select mounting, as it would look?

  • Well there depends a lot, if you are using some layout tool or if you are using code directly in html... Overall you can count the Ids within this foreach that is already there, and then a new for to create the tags <option> within the select.

  • I’m doing it right in html

Show 1 more comment

1 answer

0


For those who have the same doubt.

<?php

 $rsPA = $mysqli->query("SELECT * FROM provas_agendadas WHERE status = 'A' ");
 foreach ($rsPA as $key => $rsRowPA){
   $temp = explode("," , $rsRowPA['disciplinas']);
   foreach($temp as $elem){
    $number = explode('-',$elem);
    $number = end($number);
    $numbers[] = $number;
   }
 }
 $dis1 = array_count_values($numbers);

 echo '<pre>';print_r($dis1);echo '</pre>';

?>

Browser other questions tagged

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