第331行的form_helper中的Codeigniter form_dropdown错误

时间:2014-06-27 14:17:48

标签: codeigniter

我是codeigniter的新手,我想使用form_dropdown创建一个表单。我已经尝试过很多来自其他人的想法,但仅仅是没有做对。我想使用codeigniter重新创建我的在线酒店预订项目,因为wamp服务器2不再支持mysql扩展了,我想了解更多关于使用框架的PHP:

这是我的模特:

public function room_options()
        {
            $query = $this->db->get('room_type');

            if($query->num_rows() > 0)
            {
                foreach($query->result() as $row)
                {
                    $data[$row->room_type_id] = $row->room_type_name;
                }
                return $data;
            }
        }

        public function check_rooms($room_type)
        { 
            $this->db->where('room_type_id', $room_type);
            $this->db->where('`room_id` NOT IN (SELECT `room_id` FROM `reservation`)', NULL, FALSE);
            $query = $this->db->get('room');
            var_dump($this->db->queries);

            if($query->num_rows() > 0)
            {
                foreach($query->result() as $row)
                {
                    $data[$row->room_id] = $row->room_name;
                }
                return $data[0];
            }
        }

这是我的控制器:

public function welcome_page($page = 'welcome_page')
{
    if ($this->session->userdata('is_logged_in'))
    {
    if(! file_exists('application/views/'.$page.'.php'))
    {
        show_404();
    }
            $this->load->model('check_availability_model');
            $data['options'] = $this->check_availability_model->room_options();
            $this->load->view('includes/member_header');
            $this->load->view('includes/member_navigation');
    $this->load->view(''.$page, $data);
            $this->load->view('includes/member_footer');
    }
    else
    {
    redirect('member/restricted');
    }
}

public function restricted()
{
    $this->load->view('restricted');
}

public function register($page = 'register')
{
    if(! file_exists('application/views/'.$page.'.php'))
    {
        show_404();
    }
    $this->load->view('includes/header');
    $this->load->view(''.$page);
    $this->load->view('includes/header');
}

    public function check_reservation()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('room_type', 'Room Type', 'required|trim|xss_clean|callback_reserve_credentials');
        $this->form_validation->set_rules('checkin_date', 'Checkin Date', 'required|trim|xss_clean');
        $this->form_validation->set_rules('checkout_date', 'Checkout Date', 'required|trim|xss_clean');

        if($this->form_validation->run()==FALSE)
        {
            $this->load->model('check_availability_model');
            $data['options'] = $this->check_availability_model->room_options();
            $this->load->view('includes/member_header');
            $this->load->view('includes/member_navigation');
    $this->load->view('welcome_page', $data);
            $this->load->view('includes/member_footer');
        }
        else
        {
            redirect('member/reservation_form');
        }
    }

    public function reserve_credentials()
    {
        $this->load->model('check_availability_model');
        if($this->check_availability_model->check_rooms($this->input->post('room_type')))
        {
            return true;
        }
        else
        {
            $this->form_validation->set_message('reserve_credentials', 'The room is not available at this time. Please choose another.');
            return false;
        }
    }

public function reservation_form($page = 'reservation_form')
{
    if ($this->session->userdata('is_logged_in'))
    {
    if(! file_exists('application/views/'.$page.'.php'))
    {
        show_404();
    }
            $this->load->library('subquery');
            $this->load->model('check_availability_model');
            $data['options'] = $this->check_availability_model->check_rooms($this->input->post('room_type'));
            $this->load->view('includes/member_header');
            $this->load->view('includes/member_navigation');
    $this->load->view(''.$page, $data);
            $this->load->view('includes/member_footer');
    }
    else
    {
    redirect('member/restricted');
    }
}

和我的观点:

    <?php echo form_open('member/check_reservation', array('class' => 'form col-md-12 center-block', 'role' => 'search'));?>
    <?php echo form_dropdown('room_name', $options, set_value('room_name'), 'class="form-control input-lg"');?>
    <p></p>
    <?php echo form_submit('register_submit', 'Register', "class='btn btn-primary btn-lg btn-block'");?>
    <p></p>
    <?php echo validation_errors();?>
    <?php echo form_close();?>

用户将选择房间类型和日期。然后它将被重定向到另一种形式,用户将再次从用户之前选择的房间类型中选择一个房间名称。我试着var_dump查询,它说:

0 =&gt;字符串'SELECT * FROM(room) WHERE room_type_id = 0 AND room_id NOT IN(选择room_id FROM reservation)'(长度= 107)

必须阅读

$ this-&gt; input-&gt; post('room_type')。

Your help will really be appreciated. Thank you very much.


  [1]: http://i.stack.imgur.com/3wAy2.png

3 个答案:

答案 0 :(得分:0)

  else
    {
        return false;
    }

你需要在这里返回一个空数组。

答案 1 :(得分:0)

尝试以下几项更改...而不是在模型中使用post()函数,将其作为参数传递给check_rooms();

public function check_rooms($room_type)

您现在可以在$db->where()

中使用此功能
$this->db->where('room_type_id', $room_type);

控制器......

$data['options'] = $this->check_availability_model->check_rooms($this->input->post('room_type'));

您还应该从模型中返回数据数组的第一个索引;

return $data[0];

在浏览器中加载时查看页面源,并检查表单值是否正确。

希望这有帮助。

答案 2 :(得分:0)

所以,经过两天的调试,终于得到了我的帖子的答案。由于$ this-&gt; input-&gt; post(&#39; room_type&#39;)无法在reservation_form上读取,我需要传递变量:

我在控制器上做了一些更改:

public function check_reservation()
{
     $this->load->library('form_validation');
     $this->form_validation->set_rules('room_type', 'Room Type', 'required|trim|xss_clean|callback_reserve_credentials');
     $this->form_validation->set_rules('checkin_date', 'Checkin Date', 'required|trim|xss_clean');
     $this->form_validation->set_rules('checkout_date', 'Checkout Date', 'required|trim|xss_clean');

     if($this->form_validation->run()==FALSE)
     {
         $this->load->model('check_availability_model');
         $data['options'] = $this->check_availability_model->room_options();
         $this->load->view('includes/member_header');
         $this->load->view('includes/member_navigation');
         $this->load->view('welcome_page', $data);
         $this->load->view('includes/member_footer');
     }
     else
     {
         //so here is the change
         //redirect('member/reservation_form');
         $room_type = $this->reservation_form($this->input->post('room_type'));
     }
   }

public function reservation_form($room_type)//$this->input->post('room_type') can now be read by passing the parameter $room_type
{
    if ($this->session->userdata('is_logged_in'))
    {
    if(! file_exists('application/views/reservation_form.php'))
    {
        show_404();
    }
            $this->load->library('subquery');
            $this->load->model('check_availability_model');
            $data['options'] = $this->check_availability_model->check_rooms();
            $this->load->view('includes/member_header');
            $this->load->view('includes/member_navigation');
    $this->load->view('reservation_form', $data);
            $this->load->view('includes/member_footer');
    }
    else
    {
    redirect('member/restricted');
    }
}

我的模型仍然是相同的:

    public function check_rooms()
    {   
        $this->db->where('room_type_id', $this->input->post('room_type'));
        $this->db->where('`room_id` NOT IN (SELECT `room_id` FROM `reservation`)', NULL, FALSE);
        $query = $this->db->get('room');

        if($query->num_rows() > 0)
        {
            foreach($query->result() as $row)
            {
                $data[$row->room_id] = $row->room_name;
            }
            return $data;
        }
        else
        {
            return false;
        }
    }

如果你的答案比我的答案好,请随意添加一些建议。我也一直期待尽可能使我的代码成为捷径。再次感谢你:))