Welcome Guest, Not a member yet? Register   Sign In
Upload images along with my news items ?
#11

[eluser]invision[/eluser]
OK, I've now managed to get 90% of it working Smile

My current code.....

Controller(snippet):
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
          {
              //$data = array('upload_data' => $this->upload->data());              
              //$this->load->view('upload_success', $data);
          }    
          $this->MNews->addNews();    
          $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');    
      }
  }

Model(snippet):
Code:
function addNews(){
    $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'],
            'pubdate' => $now
        );    
    $this->db->insert('news', $data);
    }

Tiny final issue.

If I want to send the filename value to my addNews method in my News model, how would I do this?


Many thanks for any help.
#12

[eluser]farinspace[/eluser]
By the looks of your code you would use the following:

Code:
$file_name = !empty($data['upload_data']) ? $data['upload_data']['file_name'] : NULL ;
$this->MNews->addNews($file_name);

Take a look at the File Uploading Class section of the user guide (scroll to the bottom of the page, for all the params you can use):

http://ellislab.com/codeigniter/user-gui...ading.html
#13

[eluser]invision[/eluser]
Thanks for the reply.

Just to confirm, my Controller would be something like this:

Code:
...
$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
          {
              //$data = array('upload_data' => $this->upload->data());              
              //$this->load->view('upload_success', $data);
          }    
          //$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');
...

Would I need to make alts to my addNews function in the model?
Like...
Code:
'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


Thanks again for your help.
#14

[eluser]invision[/eluser]
Hey farinspace,

Can you give me a little bit more help with this?



Thank you.
#15

[eluser]farinspace[/eluser]
Your addNews function would be something like this (you'll have to test the code of course):

Code:
function addNews($file_name)
{
    $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'],
            'pubdate' => date("Y-m-d H:i:s"),
            'img_header' => $file_name
        );    
    $this->db->insert('news', $data);
}

...and remember to uncomment your code:

Code:
else
{
    $data = array('upload_data' => $this->upload->data());              
    $this->load->view('upload_success', $data);
}
#16

[eluser]invision[/eluser]
Wow, brilliant!!!!

Many thanks farinspace, you've just made my day Big Grin


Thank you




Theme © iAndrew 2016 - Forum software by © MyBB