【发布时间】:2020-06-03 12:55:26
【问题描述】:
如下面的代码所示,Semester 下拉菜单和 Programme 下拉菜单当前是分开工作的,它们不相互依赖。
如何将其更改为依赖下拉列表?
例如:当用户选择 31934 学期时,只有该学期(semester)下存在的程序(student_prg)才会显示在程序下拉列表中。
代码
<div class="modal-body">
<div class="form-group">
<label for="title">Select Current Semester:</label>
<select name="semester" class="form-control">
<option value="">--- Select Current Semester ---</option>
<?php
require('../setting/config.php');
$query = "SELECT DISTINCT semester FROM marketing_data ORDER BY semester DESC";
$do = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($do)){
echo '<option value="'.$row['student_matric'].'">'.$row['semester'].'</option>';
}
?>
</select>
</div>
<div class="form-group">
<label for="title">Select Programme:</label>
<select id="prg" name="prg" class="form-control">
<option value="">--- Select Programme ---</option>
<?php
$query2 = "SELECT student_prg, COUNT(student_prg) as count FROM marketing_data GROUP BY student_prg ORDER BY student_prg DESC";
$do = mysqli_query($conn, $query2);
while($row = mysqli_fetch_array($do)){
echo '<option value="'.$row['student_matric'].'" data-count="'.$row['count'].'">'.$row['student_prg'].'</option>';
}
?>
</select>
截图
数据库
mysql> describe marketing_data;
+---------------+------------------+------+-----+---------+--------------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+--------------------+
| student_matric| varchar(10) unsigned | NO | PRI | NULL | auto_increment |
| student_prg | text unsigned | YES | | NULL | |
| semester | varchar(10) | YES | | NULL | |
| intake_year | int(10) | YES | | NULL | |
| student_city | text | YES | | NULL | |
| city_lat | varchar(20) | YES | | NULL | |
| city_long | varchar(20) | YES | | NULL | |
| student_state | text | YES | | NULL | |
| state_code | varchar(100) | YES | | NULL | |
+---------------+------------------+------+-----+---------+--------------------+
回复评论@VPC
【问题讨论】:
-
数据集有多大?排列组合?
-
您需要一些 ajax 来根据上述下拉列表的选定值更新第二个下拉列表
-
@SaydFuad ,早些时候我尝试过 phppot.com/demo/… 的 ajax 并根据我的系统更改代码。但是在我的系统中,两个下拉列表来自同一个 MySQL 表。
-
在我看到的示例中,依赖下拉列表来自不同的 MySQL 表。例如,对于 state 下拉菜单将是 state 表,对于 city 下拉菜单将是 city table。
标签: javascript php html mysql