Unable to pass value to controller code code

advertisements

here is my code of the controller

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

here is my code of the model :

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);
}

but the model is not inserting the value $postdate to database.It is inserting the default value that is 20160824 where as the echo in controller is showing the value which is passed as parameter,i.e. the (correct) value i want to insert in the database.


You don't give default values in a function call. Rather you provide default values in the function's definition.

SanketR has correctly answered your question. I would like to make it clear.

In you controller:

You are changing the value of $postdate to 20160824. This is not the expected behaviour. change this:

$this->news_model->set_news($postdate = 20160824);

to

$this->news_model->set_news($postdate);

And in your model, change the function head to:

public function set_news($postdate = 20160824)

This would set a default value for $postdate if that variable is not provided in the function call in the controller.