Reputation: 4821
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();
}
Upvotes: 0
Views: 144
Reputation: 450
In your model just put where instead of set.
public function update_news($slug)
{
$data = array(
'slug' => $slug,
'text' => $this->input->post('text')
);
$this->db->where('slug', $slug);
$this->db->update('news', $data);
}
Upvotes: 1
Reputation: 731
If you try to load a view file that doesn't exist, you'll get a 404 error, even if the controller exists.
Did you create a 'news/success' view file?
Upvotes: 0
Reputation: 1246
I've done some small updates to your code, however I still need to see your "get_news" function to fully clarify if it's broken (by it showing the 404)
Controller:
<?php
public function view($slug) {
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item'])) {
show_404();
}
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('text', 'text', 'required');
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');
}
}
?>
Model:
<?
public function update_news($slug) {
$data = array(
'text' => $this->input->post('text')
);
$this->db->where('slug', $slug);
$this->db->update('news', $data);
}
?>
View:
<?php
echo validation_errors();
echo form_open('news/view/'.$news_item['slug']);
?>
<h2><?=$news_item['title']?></h2>
<label for="text">Text</label>
<textarea name="text"><?=$news_item['text'];?></textarea>
<br />
<input type="submit" name="submit" value="Update" />
</form>
Upvotes: 0