[eluser]Mantra of Doom[/eluser]
Hi everyone,
I feel like such a noob being stuck like this, but I'm working on a blog application and having trouble updating posts. I can view the post, click the edit link and my editing form comes up populated with data. Great! But, as soon as I hit the submit button, things seem to stop working.
I'm sure it's because my code is a little convoluted or I'm doing things the long way, but I thought I'd ask now before I tear all my hair out.
The Controller
Code:
// Update Post
function edit(){
$this->load->helper('url');
$this->load->model('news_model');
$data = $this->news_model->general();
$data['url_title'] = $this->uri->segment(3); //segment 3 is the clean url slug
$data['post'] = $this->news_model->get_entry($data['url_title']); //fetch data from this entry to fill form
if($this->input->post('submit')){
$this->news_model->update(); //calls input function to post
redirect('news/page', 'refresh'); //after hitting submit, load the news page and refresh.
}
$data['websubtitle'] = 'Edit News Post'; //change page header
$data['content'] = 'news/news_edit'; //content
$this->load->view('template', $data); //load template
}
The Model
Code:
// Gets a single entry based on its url title
public function get_entry($url_title) {
$this->db->select('entry_id, title, entry, author, creation_date, url_title')->where('url_title', $url_title);
$query = $this->db->get('news', 1);
if ($query->num_rows() == 1) {
return $query->row();
} else {
return false;
}
}
// Function to update the news post
function update(){
$this->load->database();
$data = array(
'summary'=>$this->input->post('entry'), //post first 250 characters from entry to summary
'entry'=>$this->input->post('entry'), //posts the entry
'title'=>$this->input->post('title'), //posts the title
'url_title'=>$this->input->post('url_title'), //non-editable, but from hidden form
'author'=>$this->input->post('author'), //calls author name
);
$this->db->set('creation_date', 'NOW()', FALSE); //changes post date
$this->db->where('url_title', $url_title);
$this->db->update('news',$data);
}
function general(){
$user = $this->ion_auth->get_user(); //pulls username from ion auth
$data['tab'] = 'section-1'; //This which submenu is shown
$data['summary'] = 'Summary';
$data['entry'] = 'Entry';
$data['author'] = 'Author';
$data['title'] = 'Title';
$data['url_title'] = 'URL Title';
$data['creation_date'] = 'Creation Date';
$data['fid'] = 0;
$data['fsummary'] = array('name'=>'summary',
'rows'=>5,
'cols'=>80
);
$data['fentry'] = array('name'=>'entry',
'rows'=>10,
'cols'=>80
);
$data['ftitle'] = array('name'=>'title',
'size'=>30
);
$data['furl_title'] = array('name'=>'url_title',
'size'=>30
);
// This will pull the author's name into the database when posting if the user is logged in.
if ($this->ion_auth->logged_in())
{
$data['fauthor']['value'] = $user->username;
}
else
{
$data['fauthor']['value'] = 0;
}
return $data;
}
The View
Code:
<h3 class="blog">Edit Post </h3>
<?php echo form_open('news/edit/'); ?>
<p>Title: <?php echo form_input('ftitle', $post->title);?></p>
<p>Entry:</p>
<?php echo form_hidden('author',$fauthor['value']); ?>
<?php echo form_hidden('url_title',$url_title);?>
<p><?php echo form_textarea('fentry', $post->entry); ?></p>
<p><?php echo form_submit('submit', 'Submit'); ?></p>
<?php echo form_close(); ?>
Thanks in advance for any and all help/advice!