我在 php 中收到未定义的错误
我正在尝试创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你有几个问题。
这些行不执行任何操作:
$sqlrow['建筑物']];
$sqldataArray[$sqlrow['建筑物']]['房间'][$sqlrow['房间']];
您试图在定义它的
foreach
之后引用$buildingData
关闭。当您的下一个foreach
循环尝试使用它时,它是null
,因为您超出了前面的foreach
的范围。 这会导致出现错误消息。您应该考虑从更高层次审视您的应用程序逻辑,并首先决定如何使用伪代码对其进行布局。
(编辑:SO 不允许您将代码块放入列表中?为什么?!)
You have a few issues.
These lines don't do anything:
$sqlrow['Building']];
$sqldataArray[$sqlrow['Building']]['Rooms'][$sqlrow['Room']];
You're trying to reference
$buildingData
after yourforeach
which defines it has closed. When your nextforeach
loop tries to use it, it'snull
, because you're outside the scope of the precedingforeach
. 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?!)
不完全确定您想要获得什么作为最终输出 - 您的代码存在一些逻辑问题
看看这个小重写 - 这将产生一个
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.
您没有定义任何变量:
应该是这样的:
You're not defining any variables:
Should be something like: