Select Group with PHP

Asked

Viewed 339 times

1

I’m trying to feed a select group with database data, but it’s not working. Someone can see the error?

    <div class="form-group">
       <select class="" name="agencia" tabindex="-1"  style="height: 30px;" required="">
          <option value="">Selecione sua Agência...</option> 
          <?php
          $pdo = Conexao::getInstance();

            $consulta2 = $pdo->prepare("SELECT DISTINCT(estado_agencia) FROM agencia");            
              if($consulta2->execute()){
                if($consulta2->rowCount() > 0){
                  while($data = $consulta2->fetch(PDO::FETCH_OBJ)){
                    $estado_agencia= $data->estado_agencia;
                    echo '<optgroup label="'.$estado_agencia.'">';
                    $consulta3 = $pdo->prepare("SELECT id_agencia, descricao_agencia FROM agencia WHERE estado_agencia=:estado GROUP BY estado_agencia");
                    $consulta3->bindParam(':estado',$estado_agencia, PDO::PARAM_STR);


                    while($data2 = $consulta3->fetch(PDO::FETCH_OBJ)){
                      $id_agencia = $data2->id_agencia;
                      $descricao_agencia= $data2->descricao_agencia;
                      echo '<option value="'.$id_agencia.'">'.$descricao_agencia.'>';
                      echo '</option>';
                    }
                    echo '</optgroup>';
                  }
                }
              }
          ?>
    </select>
  </div>
  • What’s the matter, you made a mistake?

  • Specify the problem so we can assist you.

  • Store data is not being shown in select, only states.

  • Are you sure you’re getting into WHILE ?

1 answer

0


Good morning @Isadora Almeida
I put here the way you did and I really couldn’t get the combo filled.
Unfortunately I can’t tell you why your code doesn’t work.
That way you did I didn’t know... :)

<body>

<div class="form-group">
    <select class="" name="agencia" tabindex="-1"  style="height: 30px;" required="" id="alimentos">                        
     <?php
         $resultado = mysql_query("SELECT estado_agencia FROM agencia group by estado_agencia");                    
         echo(' <option value="">Selecione sua Agência...</option>');                     
         echo('<optgroup>');
         while ($dados = mysql_fetch_array($resultado)) 
         {
            $estado = $dados['estado_agencia'];
            echo("<optgroup label=$estado> ");

            $resultado2 = mysql_query("SELECT * FROM agencia where (estado_agencia = '". $estado."')");                        

            while ($dados2 = mysql_fetch_array($resultado2)) 
            {
               $id_agencia = $dados2['id_agencia'];                                    
               $descricao_agencia = $dados2['descricao_agencia'];
               echo ("<option value=$id_agencia>$descricao_agencia</option>");
             }

        echo '</optgroup>';                           
        }
    ?>
    </select>
</div>      
</body>

Edited with 2 sqls.
It worked cool... Filled the combo correctly.
I hope you’ve helped in some way :)

  • Hello, forced by availability, but in my case, I’m not simply using a select tag. I’m polluting a select grouped, so the two selects used. Anyway, if you have any suggestions, thank you.

  • @Isadora Almeida.. Hi.. I repeated the example with the optgroup.. I made the 2 selects. A LOGIC is the same... :) Hugs

  • Thanks for the help, buddy. I found my bug. Missing the command execute from the second select of SQL. :D But thanks for the answers.

Browser other questions tagged

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