我有一个PHP脚本,可以动态显示我的数据库中的“课程”选项
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = '';
$con = mysqli_connect($db_host,$db_user,$db_pass, $db_name);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql = "SELECT courseID, name FROM courses";
$result = mysqli_query($con, $sql) or die("Error: ".mysqli_error($con));
while ($row = mysqli_fetch_array($result))
{
echo'<option value="'.$row['courseID'].'">'.$row['name'].'</option>';
}
?>
我的HTML(scorecard.php)页面中有一个下拉列表。
<form> <select id="selectCourse" > <option value = "">Select Course</option></select></form>
我想知道是否有人知道在我的下拉列表中显示此数据的脚本或方法。
感谢您的帮助
答案 0 :(得分:0)
echo'<option value="'.$row['courseID'].'">'.$row['name'].'</option>';
将其更改为
$courses[] = '<option value="'.$row['courseID'].'">'.$row['name'].'</option>';
以及你希望它显示在哪里
foreach($courses as $c){
echo $c;
}
答案 1 :(得分:0)
您必须在while
代码
select
循环
这是更新后的代码
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = '';
$con = mysqli_connect($db_host,$db_user,$db_pass, $db_name);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql = "SELECT courseID, name FROM courses";
$result = mysqli_query($con, $sql) or die("Error: ".mysqli_error($con));
?>
<form>
<select id="selectCourse" >
<option value = "">Select Course</option>
<?php while ($row = mysqli_fetch_array($result))
{
echo'<option value="'.$row['courseID'].'">'.$row['name'].'</option>';
} ?>
</select>