Create unique clear url slug for codeigniter and core php
1.Introduction 2.Source Code 3.Explanation 4.Comments
Introduction
Create an unique seo url link or slug using the following code in codeigniter or for core php. A clean url is an url which contains only alphabets (letters), numbers and a hyphen or an underscore. This is really useful in situations where we need SEO optimization and to be easily indexed by google search indexer. The sole purpose of creating a clean url slug is for seo. So your url will be easily indexed in google searching engine or any other search engine.
For example sites may have urls like the following. http://sitename.com/products.php?id=5 in which 5 is an unknown identifier. we dont know the purpose of 5 but it can be understood internally but not by the user. So instead of using a url like that if we have some thing like this (http://sitename.com/products/canon-digital-camera-model-3315/) then it will be easy for the user to indentify what he wants and it will be easily indexed by search engine and more over the google search result will show your link when people search for keywords which is present in that clean url slug. The url slug is created from the title of the product name you give while creating a product in the admin section. In the admin section you click create new product and will enter title, description, price and other information. This function (create_unique_clean_url_slug) will take the title and will replace all spaces with hyphen and will remove all extra chars if present other than letters, numbers, underscores and hyphen.
The following function is written for codeigniter framework and the function which is below this can be used for core php.
Source Code
function create_unique_slug($string,$table,$field='slug',$key=NULL,$value=NULL) { $t =& get_instance(); $slug = url_title($string); $slug = strtolower($slug); $i = 0; $params = array (); $params[$field] = $slug; if($key)$params["$key !="] = $value; while ($t->db->where($params)->get($table)->num_rows()) { if (!preg_match ('/-{1}[0-9]+$/', $slug )) $slug .= '-' . ++$i; else $slug = preg_replace ('/[0-9]+$/', ++$i, $slug ); $params [$field] = $slug; } return $slug; }
Explanation
This function takes 3 mandatory parameters and two optional parameters
While you are creating a new entry you will need to send only the first three parameters but when you are editing an existing entry then you need to send the last two parameters. Let us see what happens when we create an entry for the first time and while we edit.
Cconsider that your table is having fields like the following.
title (varchar), slug(varchar), description(text), price(decimal)
for example
i am creating an entry for the first time like
$slug = create_unique_clean_url_slug($title,'products','slug')
query = "insert into products set title='Canon Model 5', slug='$slug', description='$description' , price='$price'";
when i edit i will do it like this to prevent modifying the existing slug...
$slug = create_unique_clean_url_slug($title,'products','slug','id',$id);
query = "update products set title='Canon Model 5', slug='$slug', description='$description' , price='$price' where id=$id";
When you create an new entry for example as i has said already you will give a title and more information...
You need to pass the title as the first parameter. The second parameter is the table name. and the third parameter is the name of the field in which you are gonna store the clean url. in my example the field which will store the clean url will be "slug". so in your table you will have title, slug, description, price and so on for example.
This function creates a slug from the first parameter and compares it with all other existing slug's to check whether the now created one has a duplicate already. if there is no duplicate then the new slug will be inserted... else the new slug will be appended with -1 and if still there is already an entry with -1 then -2 will be appeneded to the slug.
Let me put it in action. consider you are creating an entry which is having a title "Canon SLR Model 3214" and when this is passed to this function a slug is created like canon-slr-model-3214 and is saved in the field slug. And again if you want a same title to be in your database then this time you will send the same title "Cannon SLR Model 3214" and now it will create a slug "canon-slr-model-3214" and it will compare with the existing slug. It already exists so now what will happen is this slug will be appended with -1 so the new slug will be "cannon-slr-model-3214-1" and again you create a new entry with the same title then the slug will be "cannon-slr-model-3214-2" and so on...
But mostly you will not end up with duplicate slugs. and the above para is in case if you had such exception then this function will take care of that.
Now let us see what happens while we edit. Consider that you have changed the description and want to save it.
So now again your code will create a slug before updating that row. now what happens is there is already a slug in the table and it will append -1...
if you again update the same entry your slug will change to -2 so we need to do an if condition while you edit and that is the reason for the last two parameter. using the last two parameters we will create a condition to prevent the same current slug being modified. So when creating the slug with using the last two parameters the current slug will not be altered to -1 or -2 ...
for example i am creating an entry for the first time like
$slug = create_unique_clean_url_slug($title,'products','slug')
query = "insert into products set title='Canon Model 5', slug='$slug', description='$description' , price='$price'";
when i edit i will do it like this to prevent modifying the existing slug...
$slug = create_unique_clean_url_slug($title,'products','slug','id',$id);
query = "update products set title='Canon Model 5', slug='$slug', description='$description' , price='$price' where id=$id";
oops... i think i had exaggerated
That is all folks. Enjoy.
Comments, Suggestions, Objections, ...