You need to create a temp slug_name based on the original name, and test the slug_name. On fail, recreate new slug_name based on original name. On success (breaking out of the loop) return the amended slug_name.

tutsmake
public function create_slug($id, $name)
{
    $count = 0;
    $name = url_title($name);
    $slug_name = $name;             // Create temp name
    while(true) 
    {
        $this->db->select('id');
        $this->db->where('id !=', $id);
        $this->db->where('slug', $slug_name);   // Test temp name
        $query = $this->db->get('users');
        if ($query->num_rows() == 0) break;
        $slug_name = $name . '-' . (++$count);  // Recreate new temp name
    }
    return $slug_name;      // Return temp name
}

Source :

Website name: stackoverflow

More details GO TO

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here