php错误和下拉列表需要修复

时间:2012-01-27 10:45:33

标签: php drop-down-menu

以下是数据库中的“房间”表格:

   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

我收到此错误:

  

注意:未定义的索引:第374行/web/stud/u0867587/Mobile_app/create_session.php中的房间

用于此代码:

foreach ($buildings[0]['Rooms'] as $roomId => $roomData)

所以我的问题是如何修复错误(我相信这是因为它试图找到0,即使我的建筑价值是Canalside East和Canalside West)。

以下是完整代码:

  $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 .= '<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[0]['Rooms'] as $roomId => $roomData) {        
        $roomHTML .= "<option value='".$roomId."'>" . $roomId . "</option>".PHP_EOL;        
    } 

    $roomHTML .= '</select>';

如何让下拉菜单工作,以便显示一个下拉菜单的建筑物列表,在第二个下拉列表中,它将显示第一个下拉菜单中属于所选建筑物的房间列表。此代码显示2个下拉菜单,两个菜单都只包含“请选择”选项。

2 个答案:

答案 0 :(得分:1)

你需要做更多工作才能让你按照自己的说法工作(即,当你选择一栋建筑时,你可以选择该建筑的房间)。困难在于PHP在呈现内容后不知道您在页面上选择了什么。

您当然可以从特定建筑物中获得显示房间的第二个下拉列表:

在这一行:

$buildings[$sqlrow['Building']]['Rooms'][] = $sqlrow['Room'];

您将索引设置为“Building”,而不是0.因此,$buildings[0]...正如错误所示,未定义。如果您将foreach更改为:

foreach ($buildings[<building name goes here>]['Rooms'] as $roomId => $roomData) {        
    $roomHTML .= "<option value='".$roomId."'>" . $roomId . "</option>".PHP_EOL;        
} 

应该工作。

但是,为了让页面在第一次下拉后更新第二个下拉列表,您需要使用AJAX或将第一个下拉列表发送回一个带有值的页面的响应,也许就像这样简单:

$buildingname = $_POST['buildings'];
foreach ($buildings[$buildingname]['Rooms'] as $roomId => $roomData) {        
    $roomHTML .= "<option value='".$roomId."'>" . $roomId . "</option>".PHP_EOL;        
} 

然后,您需要添加提交按钮以提交第一个选择,或者可能需要一些javascript来在所选项目更改时提交表单 -

$buildingHTML .= '<select name="buildings" id="buildingssDrop" onchange="document.getElementById(\'dropDownForm\').submit()">'.PHP_EOL;

然后,您需要将第一个select包装在表单标记中,并使用 -

的开始标记中的属性
<form action="" method="post">

希望如果你完成了所有相当糟糕的解释代码,你应该有一些工作!

(完整代码 - 未经测试 - 以防我的解释难以理解:http://pastebin.com/9UGx8nTX

答案 1 :(得分:1)

希望此解决方案能解决您的问题:

function simpleOptionBuilder($description) {
  return sprintf('<option>%s</option>'$description);
}

// ... some other code here

$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 .= '<select name="buildings" id="buildingssDrop">'.PHP_EOL;
$buildingHTML .= '<option value="">Please Select</option>'.PHP_EOL; 
join(PHP_EOL, array_map('simpleOptionBuilder', array_keys($buildings));
$buildingHTML .= '</select>';

$roomHTML = ""; 
$roomHTML .= '<select name="rooms" id="roomsDrop">'.PHP_EOL;
$roomHTML .= '<option value="">Please Select</option>'.PHP_EOL;      

$firstRow = reset($buildings);
join(PHP_EOL, array_map('simpleOptionBuilder', $firstRow['Rooms']);
$roomHTML .= '</select>';

我已将选项格式化程序封装到一个函数中,并且竞争性地将其删除了 选项标签的value属性。您不需要它,因为值和描述是相同的。

对于第一个选择,您需要建筑物名称,我认为您可以利用array_keys来利用它。

第二个选择应该是包含,如果我是对的,那么房间所属的建筑物被选入第一个。

如果你想要哈希数组的第一行,你可以使用reset function,我编写了这个解决方案。但是有一个问题。

如果第一个选择显示请选择选项,则第二个选择应为空。您需要从$ _GET,$ _POST或$ _REQUEST数组中恢复所选选项,并利用该值填充第二篇文章。您可以相应地更改代码。

// similar code above 
$selectedBuilding = $_REQUEST['buildings'];
if (isset($buildings[$selectedBuilding]) {
    join(PHP_EOL, array_map('simpleOptionBuilder', 
                            $buildings[$selectedBuilding]['Rooms']
                  )
    );
}
$roomHTML .= '</select>';

编辑:进一步封装

function simpleSelectBuilder($id, $name, $default, $optionArray) {
    $result =  '<select name="' . $name . '" id="'. $id .'">'.PHP_EOL
       . '<option value="">Please Select</option>'.PHP_EOL 
       .  join(PHP_EOL, array_map(
                'simpleOptionBuilder', 
                 $optionArray, array_fill(0,count($optionArray), $default))
          )
            .  '</select>';
}

function simpleOptionBuilder($description, $selectThisValue) {
  $selectedAttribute = $value==$selectThisValue?'selected':'';
  return sprintf('<option %s>%s</option>', $selectedAttribute, $description);
}

function param($name) {
    if (isset($_REQUEST[$name])) {
        return $_REQUEST[$name];
    }
    return null;
}


// ... some other code here

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

$selectedBuilding = param('buildings'); // TODO: sanity check
$selectedRoom = param('rooms'); // TODO: sanity check

$buildingHTML = simpleSelectBuilder(
    'buildingssDrop',
    'buildings',
    $selectedBuilding,
    array_keys($building)
); 

$roomHTML = simpleSelectBuilder(
    'roomsDrop',
    'rooms',
    $selectedRoom,
    $buildings[$selectedBuilding]['Rooms']
);

现在问题应该更清楚了:检索输入数据并相应地更新选择。您可以向第一个选择onchange事件(更简单的解决方案)添加提交请求或部署完整的ajax解决方案,但这对我来说是不可能的。