从数据库中选择带有信息的选项

时间:2018-07-05 09:34:22

标签: php jquery dropdown

我试图显示数据库表中一个选择的多个选项。

$query_lista = sqlsrv_query($conn, "SELECT * FROM inquerito");
while ($query_lista1 = sqlsrv_fetch_array($query_lista)) {
    $nome=$query_lista1["nome_inquerito"];
    $ativo=$query_lista1["ativo"];
    $id_inquerito=$query_lista1["id_inquerito"];
    echo "<select>";
    echo "<option>$nome</option>";
    echo "</select>";

因此上面的这段代码是有效的,但是可以显示多个选择,有可能只显示一个选择,并且可以在数据库中显示来自nome_inquerito的所有信息。

谢谢。

5 个答案:

答案 0 :(得分:1)

您需要在循环前移动<select>标签。

$query_lista = sqlsrv_query($conn, "SELECT * FROM inquerito");
echo "<select>";
while ($query_lista1 = sqlsrv_fetch_array($query_lista)) {
    $nome=$query_lista1["nome_inquerito"];
    $ativo=$query_lista1["ativo"];
    $id_inquerito=$query_lista1["id_inquerito"];
    echo "<option>$nome</option>";
}
echo "</select>";

类似这样的事情。

答案 1 :(得分:0)

$query_lista = sqlsrv_query($conn, "SELECT * FROM inquerito");
          echo "<select>";  
        while ($query_lista1 = sqlsrv_fetch_array($query_lista)) {
        $nome=$query_lista1["nome_inquerito"];
        $ativo=$query_lista1["ativo"];
        $id_inquerito=$query_lista1["id_inquerito"];          
        echo "<option>$nome</option>";
        }
         echo "</select>"; 

答案 2 :(得分:0)

尝试一下

select外面写while loop

<?php
$query_lista = sqlsrv_query($conn, "SELECT * FROM inquerito");
echo "<select>";
echo "<option value=''>Select Option</option>";
while ($query_lista1 = sqlsrv_fetch_array($query_lista)) {
    $nome=$query_lista1["nome_inquerito"];
    $ativo=$query_lista1["ativo"];
    $id_inquerito=$query_lista1["id_inquerito"];
    echo "<option value=".$id_inquerito.">".$nome."</option>";
}
echo "</select>";
?>

答案 3 :(得分:0)

$ conn = sqlsrv_connect($ serverName,$ connectionInfo);

///  $SQLquery  =  NOTE write sql query here

$stmt = sqlsrv_query( $conn, $SQLquery );
    if( $stmt === false) {
        die( print_r( sqlsrv_errors(), true) );
    } else {
        Display("SQLquery  executed");
    }
    $result = array();  
      $fetchLimit = 0;  // control the infinite loop 
    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {

            //echo $row['COLUMN_NAME']. "<br>";
            // Loop through each result set and add to result array
            $result[] = $row['COLUMN_NAME'];          

       // results store here  = you need to change  'COLUMN_NAME' based on yoru sql query received data name

            $fetchLimit++;
            if($fetchLimit>60000)
            break;
    }
    echo dropdown( "test", $result, 1000 );



add this function for storing in drop down 

function dropdown( $name, array $options, $selected=null )
{
    /*** begin the select ***/
    $dropdown = '<select name="'.$name.'" id="'.$name.'">'."\n";

    $selected = $selected;
    /*** loop over the options ***/
    foreach( $options as $key=>$option )
    {
        /*** assign a selected value ***/
        $select = $selected==$key ? ' selected' : null;

        /*** add each option to the dropdown ***/
        $dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
    }

    /*** close the select ***/
    $dropdown .= '</select>'."\n";

    /*** and return the completed dropdown ***/
    return $dropdown;
}

答案 4 :(得分:0)

类似这样的东西:-

<?php
$query_lista = sqlsrv_query($conn, "SELECT * FROM inquerito");
echo "<select>";
echo "<option value=''>Select Nome</option>";
while ($query_lista1 = sqlsrv_fetch_array($query_lista)) {
    $nome=$query_lista1["nome_inquerito"];
    $ativo=$query_lista1["ativo"];
    $id_inquerito=$query_lista1["id_inquerito"];
    echo "<option value=".$id_inquerito.">".$nome."</option>";
}
echo "</select>";
?>