无法将值传递给控制器函数 codeigniter
Cannot pass value to a controller function codeigniter
这是我的控制器代码
public function create($postdate)
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->helper('url');
// $postdate = $this->uri->segment(3);
echo $postdate;
$data['title'] = 'Hey there!';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('pages/create',);
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news($postdate = 20160824);
$this->load->view('pages/success');
}
}
这是我的模型代码:
public function set_news($postdate)
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text'),
'room_date' => $postdate
);
return $this->db->insert('news', $data);
}
但是模型没有将值 $postdate 插入 database.It 而是插入默认值 20160824,其中控制器中的回显显示作为参数传递的值,即我想插入数据库的(正确的)值。
你做错了。正确的方法是
public function set_news($postdate = 20160824)
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text'),
'room_date' => $postdate
);
return $this->db->insert('news', $data);
}
在你的控制器中
$this->news_model->set_news($postdate);
您不在函数调用中提供默认值。而是在函数的定义中提供默认值。
SanketR 已正确回答您的问题。我想说清楚。
在你的控制器中:
您正在将 $postdate
的值更改为 20160824
。这不是预期的行为。
改变这个:
$this->news_model->set_news($postdate = 20160824);
到
$this->news_model->set_news($postdate);
并且在您的模型中,将函数头更改为:
public function set_news($postdate = 20160824)
如果控制器的函数调用中未提供该变量,这将为 $postdate
设置默认值。
这是我的控制器代码
public function create($postdate)
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->helper('url');
// $postdate = $this->uri->segment(3);
echo $postdate;
$data['title'] = 'Hey there!';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('pages/create',);
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news($postdate = 20160824);
$this->load->view('pages/success');
}
}
这是我的模型代码:
public function set_news($postdate)
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text'),
'room_date' => $postdate
);
return $this->db->insert('news', $data);
}
但是模型没有将值 $postdate 插入 database.It 而是插入默认值 20160824,其中控制器中的回显显示作为参数传递的值,即我想插入数据库的(正确的)值。
你做错了。正确的方法是
public function set_news($postdate = 20160824)
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text'),
'room_date' => $postdate
);
return $this->db->insert('news', $data);
}
在你的控制器中
$this->news_model->set_news($postdate);
您不在函数调用中提供默认值。而是在函数的定义中提供默认值。
SanketR 已正确回答您的问题。我想说清楚。
在你的控制器中:
您正在将 $postdate
的值更改为 20160824
。这不是预期的行为。
改变这个:
$this->news_model->set_news($postdate = 20160824);
到
$this->news_model->set_news($postdate);
并且在您的模型中,将函数头更改为:
public function set_news($postdate = 20160824)
如果控制器的函数调用中未提供该变量,这将为 $postdate
设置默认值。