I am working on a basic blog application with [b]Codeigniter 3.1.8[/b] and [b]Bootstrap 4[/b].
For this application, the posts are [i]uniquely identified by slug[/i] and displayed to the viewers.
I have created a mechanism that [i]prevents duplicate slugs[/i] in case there are duplicate post titles, by adding numbers to the slugs. This mechanism is included in both the
and
methods of the Posts controller.
Inside the
method this mechanism functions imperfectly: the post slug updates even if there are no changes to the post's title ([i]my-post-title[/i] becomes [i]my-post-title-1[/i] if the "Update" button is pressed even id there is no duplicate of the post's title).
Here is the
method in the [b]Posts[/b] controller:
Code:
public function update() {
// Form data validation rules
$this->form_validation->set_rules('title', 'Title', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_rules('desc', 'Short description', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_rules('body', 'Body', 'required', array('required' => 'The %s field can not be empty'));
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
$id = $this->input->post('id');
// Update slug (from title)
if ($this->form_validation->run()) {
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$slugcount = $this->Posts_model->slug_count($slug);
if ($slugcount > 0) {
$slug = $slug."-".$slugcount;
}
} else {
$slug = $this->input->post('slug');
}
// Upload image
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['name'] != null) {
// Use name field in do_upload method
if (!$this->upload->do_upload('userfile')) {
$errors = array('error' => $this->upload->display_errors());
} else {
$data = $this->upload->data();
$post_image = $data[ 'raw_name'].$data[ 'file_ext'];
}
}
else {
$post_image = $this->input->post('postimage');
}
if ($this->form_validation->run()) {
$this->Posts_model->update_post($id, $post_image, $slug);
$this->session->set_flashdata('post_updated', 'Your post has been updated');
redirect('/' . $slug);
} else {
$this->form_validation->run();
$this->session->set_flashdata('errors', validation_errors());
redirect('/dashboard/posts/edit/' . $slug);
}
}
In the [b]Posts_model[/b] model I have:
Code:
// Count the slugs in the posts table
public function slug_count($slug){
$this->db->select('count(*) as slugcount');
$this->db->from('posts');
$this->db->where('slug', $slug);
$query = $this->db->get();
return $query->row(0)->slugcount;
}
// Update post
public function update_post($id, $post_image, $slug) {
$data = [
'title' => $this->input->post('title'),
'slug' => $slug,
'description' => $this->input->post('desc'),
'content' => $this->input->post('body'),
'post_image' => $post_image,
'cat_id' => $this->input->post('category'),
'updated_at' => date('Y-m-d H:i:s')
];
$this->db->where('id', $id);
return $this->db->update('posts', $data);
}
How shall I change the code above so that the bug described above is fixed?