以下是数据库中的“房间”表格:
Room Building Capacity
CW5/10 Canalside West 50
CW4/09 Canalside West 40
CW2/08 Canalside West 40
CW4/10 Canalside West 25
CE1/03 Canalside East 40
以下是完整代码:
<?php
foreach (array('courseid','building') as $varname) {
$$varname = (isset($_POST[$varname])) ? $_POST[$varname] : '';
}
if (isset($_POST['submit'])) {
$query = "
SELECT cm.CourseId, cm.ModuleId,
c.CourseName,
m.ModuleName
FROM Course c
INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
JOIN Module m ON cm.ModuleId = m.ModuleId
WHERE
(c.CourseId = '".mysql_real_escape_string($courseid)."')
ORDER BY c.CourseName, m.ModuleId
";
$num = mysql_num_rows($result = mysql_query($query));
if($num ==0){
echo "<p>Sorry, No Course was found with this Course ID '$courseid'</p>";
} else {
$dataArray = array();
session_start();
while ($row = mysql_fetch_array($result)) {
$dataArray[$row['CourseId']]['CourseName'] = $row['CourseName'];
$dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['ModuleName'] = $row['ModuleName'];
$_SESSION['idcourse'] = $row['CourseId'];
$_SESSION['namecourse'] = $row['CourseName'];
}
foreach ($dataArray as $courseId => $courseData) {
$output = "";
$output .= "<p><strong>Course:</strong> " . $courseId . " - " . $courseData['CourseName'] . "</p>";
$moduleHTML = "";
$moduleHTML .= '<select name="module" id="modulesDrop">'.PHP_EOL;
$moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;
foreach ($courseData['Modules'] as $moduleId => $moduleData) {
$moduleHTML .= "<option value='".$moduleId.' '.$moduleData['ModuleName']."'>" . $moduleId . " - " . $moduleData['ModuleName'] ."</option>".PHP_EOL;
}
}
$moduleHTML .= '</select>';
echo $output;
//上面是课程和模块,下面是建筑物和房间(所有代码都在一个<?php?>
标签中。
$sql="SELECT Building, Room FROM Room WHERE Building = '".$building."'";
$sqlresult = mysql_query($sql);
$buildings = array(); // easier if you don't use generic names for data
while($sqlrow = mysql_fetch_array($sqlresult))
{
// you need to initialise your building array cells
if (!isset($buildings[$sqlrow['Building']])) {
$buildings[$sqlrow['Building']] = array('Rooms' => array());
}
// you can add the room to the building 'Rooms' array
$buildings[$sqlrow['Building']]['Rooms'][] = $sqlrow['Room'];
}
$buildingHTML = "";
$buildingHTML = "<form action=\"\" method=\"post\">";
$buildingHTML .= '<select name="buildings" id="buildingssDrop" onchange="document.getElementById(\'dropDownForm\').submit()">'.PHP_EOL;
$buildingHTML .= '<option value="">Please Select</option>'.PHP_EOL;
foreach ($buildings as $building => $buildingData) {
$buildingHTML .= "<option value='".$building."'>" . $building . "</option>".PHP_EOL;
}
$buildingHTML .= '</select>';
$buildingHTML .= '</form>';
$roomHTML = "";
$roomHTML .= '<select name="rooms" id="roomsDrop">'.PHP_EOL;
$roomHTML .= '<option value="">Please Select</option>'.PHP_EOL;
if(isset($_POST['buildings'])){
$buildingname = $_POST['buildings'];
foreach ($buildings[$buildingname]['Rooms'] as $roomId => $roomData) {
$roomHTML .= "<option value='".$roomId."'>" . $roomId . "</option>".PHP_EOL;
}
}
$roomHTML .= '</select>';
?>
现在在脚本的上半部分,它控制课程和模块。用户输入一个CourseId,如果错误,它会显示一条消息,说明找不到课程,如果正确则会显示课程名称,然后会显示一个下拉菜单,其中包含属于的模块列表那个课程。这很好。
代码的下半部分是问题所在。假设显示2个下拉菜单,一个下拉列表将显示建筑物列表,在第二个下拉列表中,它将显示从第一个下拉菜单中属于所选建筑物的房间列表。此代码显示2个下拉菜单,两个菜单都只显示“请选择”选项,而不显示任何其他选项。
正如一些专家指出的那样,他们认为问题是$building
变量包含null。它是如何工作的,以便$ building变量能够从查询中检索建筑物列表?
您可以查看应用程序here输入'info101'作为courseId并提交,您将看到所有功能都出现。转到模块下拉列表,您将看到与课程相关的模块。如果您查看建筑物和房间下拉菜单,它们都只显示“请选择”。
答案 0 :(得分:2)
您需要更改PHP代码才能实现此方案。如果您希望在更改建筑物名称时动态填充房间,您可以使用jquery。在脚本中包含jQUery库。您需要执行ajax调用以动态填充列表。
我只从建筑部分添加。以上休息是一样的。在foreach循环中从数组中删除构建。将当前的PHP代码更改为此。
PHP代码:
$sql = "SELECT DISTINCT Building FROM Room";
$sqlresult = mysql_query($sql);
$buildings = array(); // easier if you don't use generic names for data
$buildingHTML = "";
$buildingHTML = "<form action=\"\" method=\"post\">";
$buildingHTML .= '<select name="buildings" id="buildingssDrop" onchange="getRooms();">'.PHP_EOL;
$buildingHTML .= '<option value="">Please Select</option>'.PHP_EOL;
while($sqlrow = mysql_fetch_array($sqlresult))
{
$building = $sqlrow['Building'];
$buildingHTML .= "<option value='".$building."'>" . $building . "</option>".PHP_EOL;
}
$buildingHTML .= '</select>';
$buildingHTML .= '</form>';
$roomHTML = "";
$roomHTML .= '<select name="rooms" id="roomsDrop">'.PHP_EOL;
$roomHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$roomHTML .= '</select>';
echo $buildingHTML;
echo $roomHTML;
您需要为动态人口添加脚本。
JS CODE:
function getRooms() {
var building = jQuery("#buildingssDrop").val();
jQuery('#roomsDrop').empty();
jQuery('#roomsDrop').html('<option value="">Please Select</option>');
jQuery.ajax({
type: "post",
url: "rooms.php",
data: "building="+building,
success: function(response){
jQuery('#roomsDrop').append(response);
}
});
}
这里的rooms.php是您需要为动态数据填充调用的ajax文件:
rooms.php
$building = isset($_POST['building']) ? $_POST['building'] : '';
$sql = "SELECT Room FROM Room WHERE Building ='".$building."'";
$sqlresult = mysql_query($sql);
$roomHTML = "";
while($sqlrow = mysql_fetch_array($sqlresult))
{
$room = $sqlrow['Room'];
$roomHTML .= "<option value='".$room."'>" . $room . "</option>".PHP_EOL;
}
echo $roomHTML;
希望这会对你有所帮助
答案 1 :(得分:0)
您确定已设置建筑物的后期价值。
因为你告诉sql输出没有显示构建参数的值,所以肯定$ building将为null。您需要将post值传递给$ building
您需要像这样更改代码:
if(isset($_POST['buildings'])){
$building = $_POST['buildings'];
} else {
$building = '';
}
$sql="SELECT Building, Room FROM Room WHERE Building = '".$building."'";
$sqlresult = mysql_query($sql);
$buildings = array(); // easier if you don't use generic names for data
while($sqlrow = mysql_fetch_array($sqlresult))
{
// you need to initialise your building array cells
if (!isset($buildings[$sqlrow['Building']])) {
$buildings[$sqlrow['Building']] = array('Rooms' => array());
}
// you can add the room to the building 'Rooms' array
$buildings[$sqlrow['Building']]['Rooms'][] = $sqlrow['Room'];
}
$buildingHTML = "";
$buildingHTML = "<form action=\"\" method=\"post\">";
$buildingHTML .= '<select name="buildings" id="buildingssDrop" onchange="document.getElementById(\'dropDownForm\').submit()">'.PHP_EOL;
$buildingHTML .= '<option value="">Please Select</option>'.PHP_EOL;
foreach ($buildings as $building => $buildingData) {
$buildingHTML .= "<option value='".$building."'>" . $building . "</option>".PHP_EOL;
}
$buildingHTML .= '</select>';
$buildingHTML .= '</form>';
$roomHTML = "";
$roomHTML .= '<select name="rooms" id="roomsDrop">'.PHP_EOL;
$roomHTML .= '<option value="">Please Select</option>'.PHP_EOL;
if(isset($_POST['buildings'])){
$buildingname = $_POST['buildings'];
foreach ($buildings[$buildingname]['Rooms'] as $roomId => $roomData) {
$roomHTML .= "<option value='".$roomId."'>" . $roomId . "</option>".PHP_EOL;
}
}
$roomHTML .= '</select>';
echo $buildingHTML;
echo $roomHTML;