List of Buildings and Rooms not appearing in dropdown list
<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>Below is the full code:</p>

<pre><code>           &lt;?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 "&lt;p&gt;Sorry, No Course was found with this Course ID '$courseid'&lt;/p&gt;";
    } 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 =&gt; $courseData) {

          $output = ""; 

          $output .= "&lt;p&gt;&lt;strong&gt;Course:&lt;/strong&gt; " . $courseId .  " - "  . $courseData['CourseName'] . "&lt;/p&gt;";

       $moduleHTML = ""; 
       $moduleHTML .= '&lt;select name="module" id="modulesDrop"&gt;'.PHP_EOL;
       $moduleHTML .= '&lt;option value=""&gt;Please Select&lt;/option&gt;'.PHP_EOL;      
            foreach ($courseData['Modules'] as $moduleId =&gt; $moduleData) {        

            $moduleHTML .= "&lt;option value='".$moduleId.' '.$moduleData['ModuleName']."'&gt;" . $moduleId . " - " . $moduleData['ModuleName'] ."&lt;/option&gt;".PHP_EOL;        
  } 
            }
            $moduleHTML .= '&lt;/select&gt;';

      echo $output;
</code></pre>

<p>//Above is course and module, below is buildings and room  (All code is in one <code>&lt;?php?&gt;</code> tag.  </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;form action=\"\" method=\"post\"&gt;";
    $buildingHTML .= '&lt;select name="buildings" id="buildingssDrop" onchange="document.getElementById(\'dropDownForm\').submit()"&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;';
    $buildingHTML .= '&lt;/form&gt;';

    $roomHTML = ""; 
    $roomHTML .= '&lt;select name="rooms" id="roomsDrop"&gt;'.PHP_EOL;
    $roomHTML .= '&lt;option value=""&gt;Please Select&lt;/option&gt;'.PHP_EOL; 

    if(isset($_POST['buildings'])){
        $buildingname = $_POST['buildings'];
    foreach ($buildings[$buildingname]['Rooms'] as $roomId =&gt; $roomData) {        
        $roomHTML .= "&lt;option value='".$roomId."'&gt;" . $roomId . "&lt;/option&gt;".PHP_EOL;        
    }
}
    $roomHTML .= '&lt;/select&gt;';


        ?&gt;
</code></pre>

<p>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.</p>

<p>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.</p>

<p>As some of the experts have pointed out, they believe the problem is that the <code>$building</code> 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?</p>

<p>You can view the application <a href="http://helios.hud.ac.uk/u0867587/Mobile_app/create_session.php" rel="nofollow">here</a> 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".</p>
