0

I want to get unique slug for my articles. I am using codeigniter. I was wondering to have some thing like sample-title-1 and sample-title-2 if there are two articles that have the same title like codeignier does with file upload filename(:num) . I could not figure out a way to do it. I am not an expert on codeigniter. I am learning it.

I prepared a function, when passed a string $str it checks if the slug exists, if it does, it adds the ID of that article to the end of that slug and returns it, if not, it returns the slug.

It is working fine and serving the purpose of unique slug. But what I wanted was to have something like sample-title-1 and sample-title-2 . Is there any way to do so?

$data['slug'] = $this->get_slug($data['title']);


public function get_slug ($str)
    {
        $slug = url_title($str, 'dash', true);
        // Do NOT validate if slug already exists
        // UNLESS it's the slug for the current page

        $id = $this->uri->segment(4);
        $this->db->where('slug', $slug);
        ! $id || $this->db->where('id !=', $id);
        $category = $this->category_m->get();

        if (count($category)) {
            return $slug.$id;
        }

        return $slug;
    }
prakashchhetri
  • 1,806
  • 4
  • 36
  • 61
  • possible duplicate of [Using slugs in codeigniter](http://stackoverflow.com/questions/3305786/using-slugs-in-codeigniter) – alpere Dec 14 '13 at 14:07

4 Answers4

1

easy to use and really helpful to create unique slugs have look on CI slug library

read its documentation to implement it.

umefarooq
  • 4,540
  • 1
  • 29
  • 38
0

what i used to do is to make slug db field UNIQUE.

Then easly do all with the CI helpers Url Helper and Text Helper

    $last_id_inserted = //get from db the last post's ID;
    $post_title = "My slug would be";
    $slug =  mb_strtolower(url_title(convert_accented_characters($post_title))).'-'.$last_id_inserted;
    echo $slug;
    //outputting my-slug-would-be-123



//insert the new post with slug

So ID will be unique and slug too.

itsme
  • 48,972
  • 96
  • 224
  • 345
0

I think you need something like that:

//Database loaded
//Text helper loaded

function post_uniq_slug($slug, $separator='-', $increment_number_at_end=FALSE) {    
    //check if the last char is a number
    //that could break this script if we don't handle it
    $last_char_is_number = is_numeric($slug[strlen($slug)-1]);
    //add a point to this slug if needed to prevent number collision..
    $slug = $slug. ($last_char_is_number && $increment_number_at_end? '.':'');

    //if slug exists already, increment it
    $i=0;
    $limit = 20; //for security reason
    while( get_instance()->db->where('slug', $slug)->count_all_results('posts') != 0) {
        //increment the slug
        $slug = increment_string($slug, $separator);

        if($i > $limit) {
            //break;
            return FALSE;
        }

        $i++;
    }

    //so now we have unique slug
    //remove the dot create because number collision
    if($last_char_is_number && $increment_number_at_end) $slug = str_replace('.','', $slug);

    return $slug;
}

Examples:

post_uniq_slug('sample'); //"sample" exists
//sample-1

post_uniq_slug('sample-2013'); //"sample-2013" exists
//sample-2013-2

post_uniq_slug('sample-2013', '-', TRUE); //increment "sample-2013"
//sample-2014

*NOT TESTED

Aurel
  • 3,682
  • 1
  • 19
  • 22
0
public function create_slug($name)
{
 $table='tradeshow';    //Write table name
 $field='slug';         //Write field name
 $slug = $name;  //Write title for slug
 $slug = url_title($name);
 $key=NULL;
 $value=NULL;       
 $i = 0;
 $params = array ();
 $params[$field] = $slug;

if($key)$params["$key !="] = $value;

while ($this->db->from($table)->where($params)->get()->num_rows())
    {  
        if (!preg_match ('/-{1}[0-9]+$/', $slug ))
        $slug .= '-' . ++$i;
        else
        $slug = preg_replace ('/[0-9]+$/', ++$i, $slug );
        $params [$field] = $slug;
    }  

    return $alias=$slug;}