[eluser]cideveloper[/eluser]
Your view looks fine. Nothing messy about it.
Where did you move the validation rules to now? A config file? That is a good way to do it. Cleans up the controller. However if you dont validate the dropdown then it wont re-populate the dropdown on validation error. even if you just do a 'trim|required'.
What SitesByJoe is saying is that it is common practice to redirect after a post submit. That way the user cannot hit refresh or F5 and then be prompted to submit the form again and then do a double submit. Redirect the user to the page that was before the page that they write the blog post. On that page put a notification bar at the top with
Code:
<?=$this->session->flashdata('message');?>
Controller
Code:
public function write () {
if ($this->form_validation->run('write_blog_post') === FALSE)
{
$data = array(
'categories' => $this->blog_model->get_categories(),
'message' => (validation_errors()) ? validation_errors() : $this->session->flashdata('message')
);
$this->_view('admin/blog_write_post', $data);
}
else
{
$result_id = $this->blog_model->insert_post()
if ($result_id){
$this->session->set_flashdata('message', 'Blog Entry Submitted');
redirect('admin/previous_page');
} else {
$this->session->set_flashdata('message', 'An Error Occured Submitting the Post');
redirect('admin/blog/write');
}
}
}
Model
Code:
public function insert_post() {
$data = array(
'title' => $this->input->post('title'),
'content' => $this->input->post('content'),
'user_id' => '1',
'posted' => date('Y-m-d H:i:s'),
'category' => $this->input->post('category')
);
$this->db->insert('blog_posts', $data);
if ($this->db->affected_rows()>0){
return TRUE;
} else {
return FALSE;
}
}
View
Code:
<h3>Write a blog post</h3>
<?php echo $message;?>
<?php
$data = array(
'id' => 'write_post'
);
echo form_open('admin/blog/write', $data);
$data = array(
'name' => 'title',
'placeholder' => 'Title',
'value' => set_value('title')
);
echo form_input($data);
$data = array(
'name' => 'content',
'placeholder' => 'Content',
'value' => set_value('content')
);
echo form_textarea($data);
echo form_label('Category', 'category');
echo form_dropdown('category', $categories, set_value('category'));
echo form_submit('submit', 'Submit Post');
?>
This is very subjective but I find once you have the pages set up properly it simplifies coding a lot
and for you order_by try this
Code:
$this->db->order_by("default", "desc");
$this->db->order_by("name", "asc");
P.S. Nothing is "stupid" We are all always learning and finding better ways to do things