Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7183399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:58:16+00:00 2026-05-28T17:58:16+00:00

I am trying to create 2 dropdown menus. One for displaying a list of

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:58:17+00:00Added an answer on May 28, 2026 at 5:58 pm

    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.

    <?php
    
    $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['Building Number 1']['Rooms'] as $roomId => $roomData) {        
        $roomHTML .= "<option value='".$roomId."'>" . $roomId . "</option>".PHP_EOL;        
    } 
    
    $roomHTML .= '</select>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a dropdown list to display all the value in
I'm trying to create a dropdown that upon changing the selection from the list
I am trying to create a dropdown menu based on Stu Nicholls' : http://www.cssplay.co.uk/menus/pro_drop8.html
I am trying create a prototype where one of the features is a dropdown
I'm trying to create a Django app where the user can make a list
I'm trying to create a dynamic list that will show item of my city
I'm trying to create a dropdown list with 'select' html tag , however ,
I am trying to create a dropdown list using struts. I want to load
I'm trying to create a drop down button and its almost working except one
Trying to create a user account in a test. But getting a Object reference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.