Below is the "Room" table in the database:
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
I am getting this error:
Notice: Undefined index: Rooms in /web/stud/u0867587/Mobile_app/create_session.php on line 374
which is for this code:
foreach ($buildings[0]['Rooms'] as $roomId => $roomData)
So my question is how can the error be fixed (I believe it is because it is trying to find 0 even though my values for building is Canalside East and Canalside West).
Below is the full code:
$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 .= '<select name="buildings" id="buildingssDrop">'.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>';
$roomHTML = "";
$roomHTML .= '<select name="rooms" id="roomsDrop">'.PHP_EOL;
$roomHTML .= '<option value="">Please Select</option>'.PHP_EOL;
foreach ($buildings[0]['Rooms'] as $roomId => $roomData) {
$roomHTML .= "<option value='".$roomId."'>" . $roomId . "</option>".PHP_EOL;
}
$roomHTML .= '</select>';
How can I get the dropdown menus to work so that it displays a list a buildings for one dropdown menu and in second dropdown it will display the list of rooms which belongs to the selected building from the first drop down menu. At the moment this code displays 2 dropdown menus, both only containing the option "Please Select".
You need to do a lot more in order to get this working as you said (ie, when you select one building you can select the rooms for that building). The difficulty is that PHP is not aware of what you have selected on the page once it has rendered the content.
You can certainly get the second dropdown to display rooms from a specific building:
On this line:
$buildings[$sqlrow['Building']]['Rooms'][] = $sqlrow['Room'];
You set the index to 'Building', not 0. For this reason, $buildings[0]...
is, as the error says, undefined. If you change the foreach to:
foreach ($buildings[<building name goes here>]['Rooms'] as $roomId => $roomData) {
$roomHTML .= "<option value='".$roomId."'>" . $roomId . "</option>".PHP_EOL;
}
Should work.
However, to get the page to update the second dropdown after the first has been changed you'd need to either use AJAX or make the first dropdown post a response back to the page with a value, maybe something as simple as this:
$buildingname = $_POST['buildings'];
foreach ($buildings[$buildingname]['Rooms'] as $roomId => $roomData) {
$roomHTML .= "<option value='".$roomId."'>" . $roomId . "</option>".PHP_EOL;
}
You'd then need to add in a submit button to submit the first select, or possibly some javascript to submit the form when the selected item is changed -
$buildingHTML .= '<select name="buildings" id="buildingssDrop" onchange="document.getElementById(\'dropDownForm\').submit()">'.PHP_EOL;
You'd then need to wrap the first select in form tags, with attributes in the opening tag of -
<form action="" method="post">
And hopefully, if you got through all of that rather badly explained code, you should have something working!
(Full code - untested - in case my explanation is too difficult to follow: http://pastebin.com/9UGx8nTX)
Hope this solution will address your problem:
function simpleOptionBuilder($description) {
return sprintf('<option>%s</option>'$description);
}
// ... some other code here
$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 .= '<select name="buildings" id="buildingssDrop">'.PHP_EOL;
$buildingHTML .= '<option value="">Please Select</option>'.PHP_EOL;
join(PHP_EOL, array_map('simpleOptionBuilder', array_keys($buildings));
$buildingHTML .= '</select>';
$roomHTML = "";
$roomHTML .= '<select name="rooms" id="roomsDrop">'.PHP_EOL;
$roomHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$firstRow = reset($buildings);
join(PHP_EOL, array_map('simpleOptionBuilder', $firstRow['Rooms']);
$roomHTML .= '</select>';
I've encapsulated the option formatter into a function, and contestually removed the
value attribute of the option tag. You don't need it as value and description are the same.
For the first select you need the Buildings names and I think you can leverage on array_keys to leverage on this.
The second select should be the contains, if I'm right, the rooms belongings to the building selected into the first one.
If you want the first row of an hash array you can use the reset function and I've coded this solution. There is an issue, though.
If the first select shows the Please Select option then the second select should be empty. You need to recover the selected option from the $_GET, $_POST or $_REQUEST arrays and leverage on that value to fill the second post. You can change the code accordingly.
// similar code above
$selectedBuilding = $_REQUEST['buildings'];
if (isset($buildings[$selectedBuilding]) {
join(PHP_EOL, array_map('simpleOptionBuilder',
$buildings[$selectedBuilding]['Rooms']
)
);
}
$roomHTML .= '</select>';
EDIT: Further Encapsulation
function simpleSelectBuilder($id, $name, $default, $optionArray) {
$result = '<select name="' . $name . '" id="'. $id .'">'.PHP_EOL
. '<option value="">Please Select</option>'.PHP_EOL
. join(PHP_EOL, array_map(
'simpleOptionBuilder',
$optionArray, array_fill(0,count($optionArray), $default))
)
. '</select>';
}
function simpleOptionBuilder($description, $selectThisValue) {
$selectedAttribute = $value==$selectThisValue?'selected':'';
return sprintf('<option %s>%s</option>', $selectedAttribute, $description);
}
function param($name) {
if (isset($_REQUEST[$name])) {
return $_REQUEST[$name];
}
return null;
}
// ... some other code here
$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'];
}
$selectedBuilding = param('buildings'); // TODO: sanity check
$selectedRoom = param('rooms'); // TODO: sanity check
$buildingHTML = simpleSelectBuilder(
'buildingssDrop',
'buildings',
$selectedBuilding,
array_keys($building)
);
$roomHTML = simpleSelectBuilder(
'roomsDrop',
'rooms',
$selectedRoom,
$buildings[$selectedBuilding]['Rooms']
);
Now the problem should be clearer: Retrieve the input data and update the select accordingly. You can add a submit request to the first select onchange event (simpler solution) or deploy a full ajax solution but this is out of the question topic to me.