Codeigniter - 传递多个参数

时间:2013-01-10 19:56:27

标签: codeigniter

我正在尝试从控制器向我的模型传递多个参数。在我提交表单后,我试图通过的$scholId似乎不会进入模型。但是,$userId可以很好地进入数据库。 $this->uri->segment(4)有什么问题无法正确传递吗?

function apply() 
{
    $this->load->helper('form');
    $this->load->library('form_validation');

    $scholId = $this->uri->segment(4);

    $userId = '2'; //User query -> this will be taken from session

    $this->data['scholarship'] = $this->scholarship_model->getScholarship($scholId);
    $this->data['title'] = "Apply";

    $this->form_validation->set_rules('essay', 'Essay', 'required');

    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('templates/header', $this->data);
        $this->load->view('student/page_head', $this->data);
        $this->load->view('student/form', $this->data);
        $this->load->view('templates/footer', $this->data);
    }else{
        // Validation passes
        $this->users_model->applySchol($userId,$scholId);
        redirect('/scholarships');
    }
}

1 个答案:

答案 0 :(得分:1)

你需要在传递它之前检查它是否存在。如:

if ($this->uri->segment(4) === FALSE)
{
    $schoId = 0; //or anything else so that you know that does not exists
}
else
{
    $schoID= $this->uri->segment(4);
}

或简单地说:

$product_id = $this->uri->segment(4, 0);
//which will return 0 if it doesn't exists.
相关问题