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

```

Below is the full code:

```
           <?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;

```

//Above is course and module, below is buildings and room (All code is in one `<?php?>` tag. 

```
 $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>';

        ?>

```

Now in the top half of the script it controls the course and module. The user enters in a CourseId and if it is wrong, it will display a message stating no course if found, if it is correct it will display the course name and then it will display a dropdown menu which contains the list of modules that belongs to that course. This works fine.

The bottom half of the code is the problem. It is suppose to display 2 drop down menus, one dropdown will show a list a buildings 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 displaying the option "Please Select" and nothing else.

As some of the experts have pointed out, they believe the problem is that the `$building` variable contains null. How is it suppose to work so that the $building variable is able to retrieve the list of buildings from the query?

You can view the application [here](http://helios.hud.ac.uk/u0867587/Mobile_app/create_session.php) Type in 'info101' for the courseId and submit it, you will see all the features appear. Go onto the module dropdown list and you will see the modules associated with the course. If you look at the Building and Room Dropdown menu, they both only display "Please Select".