Ryurial
Ryurial

Reputation: 11

Display value from database inside a Multiple Select Box

This is the basic multiple select box:

<select name="State" size="5" multiple="multiple">

    <option value="1">Kedah</option>
    <option value="2">Kelantan</option>

</select>

The question is, instead of manually typing an option value, how can I get the data from database to be use as option value.

My db looks like this :

    mysql> select sta_id,sta_name
    -> from sys_state
    -> where sta_status = '1';

+--------+----------------------------------+
| sta_id | sta_name                         |
+--------+----------------------------------+
|     01 | JOHOR                            |
|     02 | KEDAH                            |
|     03 | KELANTAN                         |
|     04 | MELAKA                           |
|     05 | NEGERI SEMBILAN                  |
|     06 | PAHANG                           |
|     07 | PULAU PINANG                     |
|     08 | PERAK                            |
|     09 | PERLIS                           |
|     10 | SELANGOR                         |
|     11 | TERENGGANU                       |
|     12 | SABAH                            |
|     13 | SARAWAK                          |
|     14 | WILAYAH PERSEKUTUAN KUALA LUMPUR |
|     15 | WILAYAH PERSEKUTUAN LABUAN       |
|     16 | WILAYAH PERSEKUTUAN PUTRAJAYA    |
+--------+----------------------------------+

Upvotes: 1

Views: 13692

Answers (7)

Veerendra
Veerendra

Reputation: 2622

<?php $qry = mysql_query("select sta_id,sta_name from sys_state where sta_status = '1'");

$dropdown ='<select name="State" size="5" multiple="multiple">';
while($row = mysql_fetch_assoc($qry)){
    $dropdown .='<option value="'.$row['sta_id'].'">'.$row['sta_name'].'</option>';
 }
$dropdown .= '</select>';
echo $dropdown;

And this will echo a dropdown with dynamic values

Upvotes: 0

LaideLawal
LaideLawal

Reputation: 77

Try this, it worked fine for me

<?php
<!-- Connect to your database-->

require 'dbconnect.php';

<!--  Fetch required data from the database-->

$sql="SELECT sta_id,sta_name from sys_state WHERE sta_status = '1' ";

$result = mysqli_query($con, $sql);

?>
<select multiple size="6">

<?php while($row = mysqli_fetch_assoc($result)){?>
          <option value="<?php echo $row['sta_id']; ?>"> <?php echo $row['sta_name']; ?></option>

 <?php
}
?>
 </select> 

Upvotes: 0

Ryurial
Ryurial

Reputation: 11

I managed to do it this way though:

<?php     // MULTI SELECTION 



    $sql1 = " SELECT sta_id,sta_name FROM dof_perkhidmatan.sys_state WHERE sta_status = '1' ";

    $result1 =  $db->sql_query($sql1,END_TRANSACTION);

    if($db->sql_numrows($result1) > 0)
    {

    ?>

     <select id="State" name="State" size="16" multiple="multiple">
     <?php

    // output data of each row
    while($row_select = $db->sql_fetchrow($result1))        
        {
         echo '<option value="'.$row_select['sta_id'].'">'.$row_select['sta_name'].'</option>';
        }


    ?>
    </select>
    <?php
    }
    ?>

Upvotes: 0

RBS
RBS

Reputation: 108

  1. Firstly, you should connect to DB PHP Mysql Connection
  2. Next step is to fetch those rows and write data to the variable(it can be an array or any object, in my case it's an array) PHP MySQL Data Select:

    $sql = "SELECT sta_id,sta_name FROM sys_state WHERE sta_status = '1'";
    $result = $conn->query($sql);
    $resultData = [];
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
             $resultData[$row['sta_id']] = $row['sta_name'];
        }
    }
    
  3. Finally, print them on html page:

    <select name="State" size="5" multiple="multiple">
    <?php
    foreach ($resultData as $key => $value) {
        echo '<option value="'.$key.'">'.$value.'</option>';
    }
    ?>
    </select>
    

Upvotes: 4

Fevly Pallar
Fevly Pallar

Reputation: 3109

Well, basically you can just echo them

$dsn = "mysql:host=localhost;dbname=x"; 
$user="root";
$password="";
try{
$connect = new PDO($dsn,$user,$password); // PDO= PHP Data Object
$connect->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){echo $e->getMessage();}


$select = $connect->query("select sta_id,sta_name from sys_state where sta_status = '1';"); 

echo "<select name=\"State\" size=\"5\" multiple=\"multiple\">";

foreach ($select as $temp){


   echo "<option value=\"1\">".temp["sta_name"]."  </option>";
    ......
    ....


}
echo "</select>";

$connect=null;// CLOSE CONNECTION

but constructing them into a function would be better

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31749

Try with -

$mysqli = new mysqli("localhost", "user", "password", "database");
$res = $mysqli->query("select sta_id,sta_name from sys_state where sta_status = '1'");

?>
<select name="State" size="5" multiple="multiple">
<?php

while ($row = $res->fetch_assoc()) {
    echo "<option value='".$row['sta_id']."'>".$row['sta_name']."</option>";
}
?>
</select>

Upvotes: 0

Rakesh Sharma
Rakesh Sharma

Reputation: 13738

Try

<?php $qry = mysql_query("select sta_id,sta_name from sys_state where sta_status = '1'");?>

<select name="State" size="5" multiple="multiple">
<?php while($row = mysql_fetch_assoc($qry)){?>
    <option value="<?php echo $row['sta_id'];?>"><?php echo $row['sta_name'];?></option>
 <?php }?>
</select>

Note :- mysql_* has been deprecated use mysqli_* or PDO

Upvotes: 0

Related Questions