I am trying to create 2 dropdown menus. One for displaying a list of buildings and then when user selects a building from the list, it will display the list of rooms in that building.
Problem is I have an error in my code. Below is the code:
$sql="SELECT Building, Room FROM Room WHERE Building = '".$building."'";
$sqlresult = mysql_query($sql);
$sqldataArray = array();
while($sqlrow = mysql_fetch_array($sqlresult))
{
$sqldataArray[$sqlrow['Building']];
$sqldataArray[$sqlrow['Building']]['Rooms'][$sqlrow['Room']];
}
$buildingHTML = "";
$buildingHTML .= '<select name="buildings" id="buildingssDrop">'.PHP_EOL;
$buildingHTML .= '<option value="">Please Select</option>'.PHP_EOL;
foreach ($sqldataArray as $building => $buildingData) {
$buildingHTML .= "<option value='".$building."'>" . $building . "</option>".PHP_EOL;
}
$buildingHTML .= '</select>';
$roomHTML = "";
$roomHTML .= '<select name="rooms" id="roomsDrop">'.PHP_EOL;
$roomHTML .= '<option value="">Please Select</option>'.PHP_EOL;
foreach ($buildingData['Rooms'] as $roomId => $roomData) {
$roomHTML .= "<option value='".$roomId."'>" . $roomId . "</option>".PHP_EOL;
}
$roomHTML .= '</select>';
The error I am getting is this:
Undefined variable: buildingData in /web/stud/u0867587/Mobile_app/create_session.php on line 372
This is the line of code where the error is:
$buildingHTML .= "<option value='".$building"'>" . $building . "</option>".PHP_EOL;
Does anyone know how to fix this error. I believe it is because it is not in the other foreach loop but if I put that in, then does it affect the display of the dropdown menu?
Not entirely sure what you’re trying to get as a final output – there’s a few logical issues with your code
Take a look at this minor rewrite – this will produce a <select> for buildings, and a <select> for all the rooms in your first building.