public function edit() { $this->load->helper('form'); $this->load->library('form_validation'); $id = $this->uri->segment(3); $post = $this->post->get_by_id($id); $this->form_validation->set_rules('title', 'Title', 'trim|required|max_length[255]'); $this->form_validation->set_rules('body', 'Body', 'trim|required|max_length[500]'); $data['action'] = site_url('blog/edit/' . $id); $data['post'] = $post; if ($_POST) { if ($this->form_validation->run() == FALSE) { $this->load->view('templates/header'); $this->load->view('blog/edit', $data); $this->load->view('templates/footer'); } else { $this->load->helper('security'); $post = new Post_model(); $post->id = xss_clean($this->uri->segment(3)); $post->title = xss_clean($this->input->post('title', TRUE)); $post->body = xss_clean($this->input->post('body', TRUE)); $date = new DateTime(); $post->created = $date->format('Y-m-d H:i:s'); if ($post->save()) { redirect(base_url('blog/details/' . $post->id), 'location'); } } } else { $this->load->view('templates/header'); $this->load->view('blog/edit', $data); $this->load->view('templates/footer'); } }
public function update() { if ($_POST) { // Build post object $post = new Post_model(); $post->id = $this->uri->segment(3); $post->title = $this->input->post('title', TRUE); $post->content = $this->input->post('content', TRUE); // Save post to database if ($post->save()) { redirect(base_url(), 'location'); } } // Get post from database $id = $this->uri->segment(3); $post = $this->post->getById($id); // Initialize form $this->load->helper('form'); $data['action'] = site_url('blog/update/' . $id); $data['title'] = $post->title; $data['content'] = $post->content; // Load views $this->load->view('header'); $this->load->view('upsert', $data); $this->load->view('footer'); }