我在 php 中收到未定义的错误

发布于 2024-12-29 08:34:36 字数 1648 浏览 0 评论 0原文

我正在尝试创建 2 个下拉菜单。一个用于显示建筑物列表,然后当用户从列表中选择建筑物时,它将显示该建筑物中的房间列表。

问题是我的代码中有错误。下面是代码:

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

我收到的错误是这样的:

未定义的变量:第372行/web/stud/u0867587/Mobile_app/create_session.php中的buildingData

这是错误所在的代码行:

$buildingHTML .= "<option value='".$building"'>" . $building . "</option>".PHP_EOL;

有谁知道如何修复这个错误。我相信这是因为它不在另一个 foreach 循环中,但如果我将其放入,那么它会影响下拉菜单的显示吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甚是思念 2025-01-05 08:34:36

你有几个问题。

  1. 这些行不执行任何操作:

    • $sqlrow['建筑物']];
    • $sqldataArray[$sqlrow['建筑物']]['房间'][$sqlrow['房间']];
  2. 您试图在定义它的 foreach 之后引用 $buildingData关闭。当您的下一个 foreach 循环尝试使用它时,它是 null,因为您超出了前面的 foreach 的范围。 这会导致出现错误消息。

您应该考虑从更高层次审视您的应用程序逻辑,并首先决定如何使用伪代码对其进行布局。

(编辑:SO 不允许您将代码块放入列表中?为什么?!)

You have a few issues.

  1. These lines don't do anything:

    • $sqlrow['Building']];
    • $sqldataArray[$sqlrow['Building']]['Rooms'][$sqlrow['Room']];
  2. You're trying to reference $buildingData after your foreach which defines it has closed. When your next foreach loop tries to use it, it's null, because you're outside the scope of the preceding foreach. This is causing your error message.

You should consider taking a higher-level look at your application logic and decide how it should be laid out with pseudocode first.

(edit: SO doesn't let you put code blocks inside lists? why?!)

美人如玉 2025-01-05 08:34:36

不完全确定您想要获得什么作为最终输出 - 您的代码存在一些逻辑问题

看看这个小重写 - 这将产生一个

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

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>';
娇纵 2025-01-05 08:34:36

您没有定义任何变量:

  $sqldataArray[$sqlrow['Building']]; 
  $sqldataArray[$sqlrow['Building']]['Rooms'][$sqlrow['Room']];

应该是这样的:

  $sqldataArray[$sqlrow['Building']] = "Value"; 
  $sqldataArray[$sqlrow['Building']]['Rooms'][$sqlrow['Room']] = "Definition"; 

You're not defining any variables:

  $sqldataArray[$sqlrow['Building']]; 
  $sqldataArray[$sqlrow['Building']]['Rooms'][$sqlrow['Room']];

Should be something like:

  $sqldataArray[$sqlrow['Building']] = "Value"; 
  $sqldataArray[$sqlrow['Building']]['Rooms'][$sqlrow['Room']] = "Definition"; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文