这是基本的多选框:
<select name="State" size="5" multiple="multiple">
<option value="1">Kedah</option>
<option value="2">Kelantan</option>
</select>
问题是,如何从数据库中获取选项值,而不是手动输入选项值。
我的数据库看起来像这样:
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 |
+--------+----------------------------------+
答案 0 :(得分:3)
下一步是获取这些行并将数据写入变量(它可以是数组或任何对象,在我的例子中它是一个数组)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'];
}
}
最后,在html页面上打印:
<select name="State" size="5" multiple="multiple">
<?php
foreach ($resultData as $key => $value) {
echo '<option value="'.$key.'">'.$value.'</option>';
}
?>
</select>
答案 1 :(得分:0)
尝试
<?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>
注意: - mysql_*
已被弃用,使用mysqli_*
或PDO
答案 2 :(得分:0)
尝试 -
$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>
答案 3 :(得分:0)
<?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;
这将回显动态值的下拉列表
答案 4 :(得分:0)
嗯,基本上你只能echo
他们
$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
但将它们构建成函数会更好
答案 5 :(得分:0)
我设法这样做了:
<?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
}
?>
答案 6 :(得分:0)
试试这个,它对我来说很好用
<?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>