ComboBox Problems

0

Good afternoon. I have a problem with a select to bring to list to choose the name of a worker from a table, but I read the guides and can not find the detail, because when testing it on the web, only the empty space appears, the part of the script

<li id="li_2" >
        <label class="description" for="element_2">NOMBRE DEL ELEMENTO </label>
        <div>

        <select>
        <?php
        $mysqli=mysqli_connect("localhost", "root", "XXXX", "pcsdb")or 
        die("No se conecto al servidor");
        if (!$conexion){ echo (mysqli_errno());

          $consulta="SELECT id,nombre FROM personal ORDER BY id ASC";
          $resultado=mysqli_query($mysqli, $consulta);                                  
          ?>                                                
          <option value="">Seleccionar</option>
          <?php
          while($fila=mysqli_fetch_array($resultado)){
          echo $fila['id'];
          ?>    
          <option value="<?php echo $fila['id'];?>"><?php echo$fila['nombre'];?></option>
          <?php
          }
          ?>

      </select>
        </div> 
        </li>

This is how it appears to me now when running in the browser

Thanks in advance

    
asked by HYDE 14.08.2017 в 22:10
source

2 answers

0

What you can do is use PHP to print all the options at once.

<?php
    while($fila=mysqli_fetch_array($resultado)){
        echo "<option value=".$fila['id'].">".$fila['nombre']."</option>";
    }
?>

Now, I think your code does not work because you're iterating over your results and not about the option tag and I think that's why it goes blank.

And that way you would see the complete code.

<li id="li_2" >
    <label class="description" for="element_2">NOMBRE DEL ELEMENTO </label>
    <div>
        <select>
        <?php
            $mysqli=mysqli_connect("localhost", "root", "XXXX", "pcsdb")or 
                die("No se conecto al servidor");
            if (!$conexion){ echo (mysqli_errno());
            $consulta="SELECT id,nombre FROM personal ORDER BY id ASC";
            $resultado=mysqli_query($mysqli, $consulta);                                  
        ?>                                                
        <option value="">Seleccionar</option>
        <?php
           while($fila=mysqli_fetch_array($resultado)){
               echo "<option value=".$fila['id'].">".$fila['nombre']."</option>";
           }
        ?>
        </select>
    </div> 
</li>
    
answered by 14.08.2017 в 22:51
0

the block where you create the option elements is conditioned to the cycle while you see how this condition does not happen, it does not return anything, try to make a var_dump or a print_r of the row so you can see if it is an error in the database or It's your php code.

    //Podrias hacerlo de esta manera
     <?php
       do{
         $fila=mysqli_fetch_array($resultado);
         //Aki podras ver que tiene almacenado la variable fila en la primera iteración
         var_dump($fila);  // o print_r($fila);
         echo "<option value=".$fila['id'].">".$fila['nombre']."</option>";
       while($fila);
    ?>
    
answered by 15.08.2017 в 23:30