php错误和下拉菜单需要修复 [英] php error and dropdown needs fixing

查看:91
本文介绍了php错误和下拉菜单需要修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below is the "Room" table in the database:

   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中的房间

Notice: Undefined index: Rooms in /web/stud/u0867587/Mobile_app/create_session.php on line 374

这是代码:

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

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

So my question is how can the error be fixed (I believe it is because it is trying to find 0 even though my values for building is Canalside East and 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>';

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

How can I get the dropdown menus to work so that it displays a list a buildings for one dropdown menu 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 containing the option "Please Select".

推荐答案

希望这个解决方案将解决你的问题:

Hope this solution will address your problem:

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

我已经将选项格式化程序一个函数,并且相互删除了选项标签的
值属性,你不需要它的值和描述是一样的。

I've encapsulated the option formatter into a function, and contestually removed the value attribute of the option tag. You don't need it as value and description are the same.

对于第一个选择你需要建筑物名称,我想你可以利用array_keys来利用这个。

For the first select you need the Buildings names and I think you can leverage on array_keys to leverage on this.

第二个选择应该是包含,如果我是对的,房间财物的物品被选入第一个。

The second select should be the contains, if I'm right, the rooms belongings to the building selected into the first one.

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

If you want the first row of an hash array you can use the reset function and I've coded this solution. There is an issue, though.

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

If the first select shows the Please Select option then the second select should be empty. You need to recover the selected option from the $_GET, $_POST or $_REQUEST arrays and leverage on that value to fill the second post. You can change the code accordingly.

// 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']
);

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

Now the problem should be clearer: Retrieve the input data and update the select accordingly. You can add a submit request to the first select onchange event (simpler solution) or deploy a full ajax solution but this is out of the question topic to me.

这篇关于php错误和下拉菜单需要修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆