Welcome Guest, Not a member yet? Register   Sign In
Send filename value from Controller to Model function?
#1

[eluser]invision[/eluser]
Hello,

I am currently using the following to create a news entry in my database.
The Controller also moves a file to my server.
This works great.

However, I'd like it so when the news entry is created, the $file_name value is inserted into the news table along with the rest of the post data.

Can anyone please help me to achieve this?

Model
Code:
function addNews($file_name){
    $now = date("Y-m-d H:i:s");
    $data = array(
            'title' => $this->input->post('title'),
            'slug' => $this->input->post('slug'),
            'status' => $this->input->post('status'),
            'body' => $this->input->post('body'),
            'category_id' => '1',
            'user_id' => $_SESSION['userid'],
            'img_header' => '1' // this is where my file name should go, instead of '1'
        );    
    $this->db->insert('news', $data);
    }

Controller
Code:
function create(){
       if ($this->input->post('title')){
                
        $config['upload_path'] = '../uploads/';
          $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size']    = '100';
          $config['max_width']  = '1024';
          $config['max_height']  = '768';
          
          $this->load->library('upload', $config);
          $this->upload->initialize($config);
          
          if (!$this->upload->do_upload())
          {
              $error = array('error' => $this->upload->display_errors());
              $this->load->view('upload_form', $error);
          }    
          else
          {
          }    
          //$this->MNews->addNews();
          $file_name = !empty($data['upload_data']) ? $data['upload_data']['file_name'] : NULL ;
          $this->MNews->addNews($file_name);

          $this->session->set_flashdata('message','News created');
          redirect('admin/news/index','refresh');
          
        } else {
          $data['title'] = "Create News";
          $data['main'] = 'admin_news_create';
          $this->load->vars($data);
          $this->load->view('dashboard');    
      }
  }


Many thanks for any help you can provide.
#2

[eluser]danmontgomery[/eluser]
Code:
function addNews($file_name){
    $now = date("Y-m-d H:i:s");
    $data = array(
            'title' => $this->input->post('title'),
            'slug' => $this->input->post('slug'),
            'status' => $this->input->post('status'),
            'body' => $this->input->post('body'),
            'category_id' => '1',
            'user_id' => $_SESSION['userid'],
            'img_header' => $file_name // this is where my file name should go, instead of '1'
        );    
    $this->db->insert('news', $data);
}

I must be missing something?
#3

[eluser]invision[/eluser]
Thanks noctrum. I made a very simple mistake at my end and it didn't work out.

Yours works perfectly. Many thanks for your help.




Theme © iAndrew 2016 - Forum software by © MyBB