1

我有一个在 CodeIgniter 的表单助手中生成的<select>包含 3 个项目的框<option>

<select onchange="this.form.submit()" name="selectCatsByComponent">
    <option value="0" selected="selected">-- Choose a component --</option>
    <option value="1">Content</option>
    <option value="2">E-commerce</option>
</select>

当我从下拉列表ContentE-Commerce<option>中选择选项时,它会以适当的值正确重定向到带有categories/get_categories/$ID. 但是,每当我选择第一个选项时,它实际上也有一个value="0",它会将我重定向到categories/get_categories/没有 ID 的位置,而不是将我重定向到categories/get_categories/0......我就是无法解决问题。

这是我的控制器Categories.php

public function get_categories($pid = 0, $com_id = 0){
    ...

    // Check if Filter sends some POST data
    if( $this->input->post('selectCatsByComponent') ){
        echo '1';
        $com_id = $this->input->post('selectCatsByComponent');
        $this->session->set_userdata(array('selectCatsByComponent' => $com_id));
    }else
        echo '2';
        $this->session->set_userdata(array('selectCatsByComponent' => $com_id));

    // Get processed data results
    $componentData['selectedComponent'] = $com_id;
    $componentData['selectComponents']  = $this->component_model->get_components();
    $componentData['items']             = $this->category_model->getCategoryList($pid, $com_id);

    ...
}

并且categories_model.php

public function get_categories($parent = FALSE, $com_id = FALSE){

    // SQL command
    $this->db->select('id, com_id');
    $this->db->order_by('categories.ordering', 'ASC');

    // Check if parent ID exist
    if( $parent !== FALSE ) $this->db->where('pid', $parent);

    // Check if component ID exist
    if( $com_id != FALSE ) $this->db->where('com_id', $com_id);

    // Alphabetize results if no ordering present
    $this->db->order_by('title', 'ASC');

    $query = $this->db->get('categories');

    // Check row existance in DB table
    if( $query->result() < 1 ) return FALSE;

    // Get results in object type
    $result = $query->result();

    $categories = array();
    $components = array();
    foreach($result as $cat){
        $categories[] = $this->get_category($cat->id);
        $components[] = $this->get_component($cat->com_id);

    }

    return $categories;
}

请问,有人能告诉我我在这里做错了什么吗?为什么它不会发布表单?在控制器的函数中,我做了一个条件语句,我在其中回显了数字。每当我选择它显示的选项时 me echo 1,除了下拉列表中的第一个选项,它显示 me echo 2。这里有什么问题?

4

1 回答 1

3

if( $this->input->post('selectCatsByComponent') )

should become

if( $this->input->post('selectCatsByComponent') !== false )

because 0 is considered false in PHP.

Small update:

Here is a list of everything that PHP considers false and where you should use comparison by type (also known as strict comparison). A nice article here.

于 2013-05-23T19:00:08.607 回答