• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter 3 blogging application bug: post slug updates even if there are no change

#1
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 

Code:
create()
 and 

Code:
update()
 methods of the Posts controller.
Inside the 

Code:
update()
 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 

Code:
update()
 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?
Reply

#2
Personally I would scrap this idea and always append the primary key to the url. That's a pretty normal practice.

1-your-title
2-your-title
3-your-title
4-your-title

So that you don't need to worry about making a select every time.

And here's the fix. Only update if there are more than one. As you are updating, there are already one present.
Code:
if ($slugcount > 1) {
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.