Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- There is a method for updating posts:
- public function update() {
- // Form data validation rules
- // irrelevant for the question suppressed
- $id = $this->input->post('id');
- // Upload image
- $config['upload_path'] = './assets/img/posts';
- $config['allowed_types'] = 'jpg|png';
- $config['max_size'] = '2048';
- $this->load->library('upload', $config);
- $post_image = ''; // set default value to empty
- if (!empty($_FILES['userfile'])) {
- $data = array('upload_data' => $this->upload->data());
- $this->upload->do_upload();
- $post_image = $_FILES['userfile']['name'];
- }
- if ($this->form_validation->run()) {
- // so this will pass empty to update_post if no upload
- $this->Posts_model->update_post($id, $post_image);
- redirect('posts/post/' . $id);
- } else {
- $this->edit($id);
- }
- }
- // change your
- public function update_post($id, $post_image) {
- $data = [
- 'title' => $this->input->post('title'),
- 'description' => $this->input->post('desc'),
- 'content' => $this->input->post('body'),
- 'cat_id' => $this->input->post('category')
- ];
- if (!empty($post_image)) {
- // push $post_image to $data only if it's not empty
- $data['post_image'] = $post_image,
- }
- $this->db->where('id', $id);
- // so this will only update other keys and not post_image if $data['post_image'] is not set
- return $this->db->update('posts', $data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement