I finished the tutorial for making a news application on the codeigniter user_guide. I am now trying to extend the tutorial website so it loads text into a textarea that can be retyped and used to update the entry. I want the view article page to be this same page for updating. The view loads correctly, but when I click submit, it brings me from localhost/ci/news/foo to localhost/ci/news/view along with a 404. I have mostly tried to imitate the create, and only altering the model query, but since I've been struggling with it for a while, have tried several ideas.
I suspect the problem could be one of the following.
Something directing me to ci/news/view Misusing my $slug variable Misconnecting the controller and model Syntax in my model query.
I'll post all the relevent code.
Here's my controller function for the view, which is also where updates will happen.
public function view($slug)
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['news_item'] = $this->news_model->get_news($slug);
$slug = $slug;
$this->form_validation->set_rules('text', 'text', 'required');
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
else
{
$this->news_model->update_news();
$this->load->view('news/success');
}
}
Here's my update_news() in the model
public function update_news($slug)
{
$data = array(
'slug' => $slug,
'text' => $this->input->post('text')
);
$this->db->set('slug', $slug);
$this->db->update('news', $data);
}
and my view.php
<?php
echo validation_errors();
echo form_open('news/view');
echo '<h2>'.$news_item['title'].'</h2>';
?>
<label for="text">Text</label>
<textarea name="text"><?php echo $news_item['text']; ?></textarea><br />
<input type="submit" name="submit" value="Update" />
</form>
UPDATE: as requested, my get_news model function.
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}