upload image in a new post save file path to db confusion codeigniter
ive made the news post were you can upload just text with a title and some
text.. now i want to expand it and add in an image upload if the user
wants to add it in... ive made an image upload were it stores in a folder
images
now i want to interpret so its all together so you can add in a text and
title and can add in an image....
now i know all i need to figue out is how to combined them in a form and
then upload the source file of the image were it uploaded so it displays
the image in the news post
News - Controller
public function create(){
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$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('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
News model
public function set_news()
{
$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')
);
return $this->db->insert('news', $data);
}
Create form
<?php echo validation_errors(); ?>
<?php echo form_open('news/create') ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="file" name="userfile" />
<input type="submit" name="submit" value="Create news item" />
</form>
Now for the image upload i have done controller
public function upload(){
$config['upload_path'] = "./images/";
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$this->load->library('upload',$config);
if(!$this->upload->do_upload()){
$error = array('error'=>$this->upload->display_errors());
$this->load->view('main_view', $error);
} else {
$file_data = $this->upload->data();
$data['img'] = base_url(). '/images/' .$file_data['file_name'];
$this->load->view('success_msg',$data);
}
}
I just need to interpret this so the file upload source save to the db and
you create the post with the image
No comments:
Post a Comment