Note that there are some explanatory texts on larger screens.

plurals
  1. POphp error and dropdown needs fixing
    primarykey
    data
    text
    <p>Below is the "Room" table in the database:</p> <pre><code> 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 </code></pre> <p>I am getting this error: </p> <blockquote> <p>Notice: Undefined index: Rooms in /web/stud/u0867587/Mobile_app/create_session.php on line 374</p> </blockquote> <p>which is for this code: </p> <pre><code>foreach ($buildings[0]['Rooms'] as $roomId =&gt; $roomData) </code></pre> <p>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).</p> <p>Below is the full code:</p> <pre><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' =&gt; array()); } // you can add the room to the building 'Rooms' array $buildings[$sqlrow['Building']]['Rooms'][] = $sqlrow['Room']; } $buildingHTML = ""; $buildingHTML .= '&lt;select name="buildings" id="buildingssDrop"&gt;'.PHP_EOL; $buildingHTML .= '&lt;option value=""&gt;Please Select&lt;/option&gt;'.PHP_EOL; foreach ($buildings as $building =&gt; $buildingData) { $buildingHTML .= "&lt;option value='".$building."'&gt;" . $building . "&lt;/option&gt;".PHP_EOL; } $buildingHTML .= '&lt;/select&gt;'; $roomHTML = ""; $roomHTML .= '&lt;select name="rooms" id="roomsDrop"&gt;'.PHP_EOL; $roomHTML .= '&lt;option value=""&gt;Please Select&lt;/option&gt;'.PHP_EOL; foreach ($buildings[0]['Rooms'] as $roomId =&gt; $roomData) { $roomHTML .= "&lt;option value='".$roomId."'&gt;" . $roomId . "&lt;/option&gt;".PHP_EOL; } $roomHTML .= '&lt;/select&gt;'; </code></pre> <p>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".</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload