Create dropdownlist from value in database php

2019-06-09 18:44发布

问题:

I'm trying to display a list of name (from oracle db) that user can choose as their supervisor at my form.

This code below only display one letter from the first name in db. eg: The first name in db is 'nanie', but it display 'n' only.

Please help me to correct this code.

    <?php 
    $sql3= oci_parse($conn,"SELECT stfname FROM staff") or die ('None'); 
    $result3=oci_execute($sql3); 
    $row3 = oci_fetch_array($sql3,OCI_ASSOC+OCI_RETURN_NULLS);  
    echo $row3['STFNAME'];
    ?>

    <td><strong>Co-supervisor : </strong></td>
    <td width="309">
        <?php echo '<select name="cosupervisor">'; 
            foreach($row3 as $value)
        {
              echo '<option value="'.$value['STFNAME'].'">'.$value['STFNAME'].'</option>';                          } 
        echo '</select>'; ?>
    </td>

回答1:

You can use foreach loop through while as: Co-supervisor :

    <?php 
    echo '<select name="cosupervisor">'; 
      while($row3 = oci_fetch_array($sql3,OCI_ASSOC+OCI_RETURN_NULLS))
    {
        $data[] = $row3 ;                         
    } 

     foreach($data as $value)
        {
              echo '<option value="'.$value['STFNAME'].'">'.$value['STFNAME'].'</option>';                          } 
        echo '</select>'; ?>
    </td> 

   ?>
</td>


回答2:

use while instead of foreach loop

<?php 
$sql3= oci_parse($conn,"SELECT stfname FROM staff") or die ('None'); 
$result3=oci_execute($sql3); 
//$row3 = oci_fetch_array($sql3,OCI_ASSOC+OCI_RETURN_NULLS);  
//echo $row3['STFNAME'];
?>

<td><strong>Co-supervisor : </strong></td>
<td width="309">
    <?php 
    echo '<select name="cosupervisor">'; 
      while($row3 = oci_fetch_array($sql3,OCI_ASSOC+OCI_RETURN_NULLS))
    {
        echo '<option value="'.$row3['stfname'].'">'.$row3['stfname'].'</option>';                          
    } 

    echo '</select>'; 

   ?>
</td>