Hi, friends. I am back with a new and interesting tutorial for you.This tutorial help you to generate SEO friendly URL slug. If you are working with PHP or Codeigniter then this tutorial will so much helpful for you.. I will tell you first that this tutorial for those people who develop their website using PHP.
What is URL slug:
For a blog site url slug is the most improtant to search a post. Because a url slug is detected the address of a particular post or a page on your website. A SEO friendly URL slug is the part of a URL that is collection from post title or page title. For example: “php-database-connection”.
Generate SEO friendly URL slug from POST title:
If you want to generate dynamically and SEO friendly URL slug then follow my steps, you can create a good SEO friendly URL slug from your post title.
Create MySQL table:
Follow the below instruction and create a table with these field.
CREATE TABLE `blog` ( `id` int(11) NOT NULL AUTO_INCREMENT, `post_title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `url_slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `post_category_id` int(11) COLLATE utf8_unicode_ci NOT NULL, `post_image` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `post_description` text(1000) COLLATE utf8_unicode_ci NOT NULL, `created_date` datetime NOT NULL, `update_date` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Insert POST (Controller)
<?php public function insert_blog(){ $url_slug=$this->generate_url_slug($this->input->post('post_title'),'blog'); $config['upload_path'] = './assets/images/post_image'; $config['allowed_types'] = '*'; $this->load->library('upload', $config); $this->upload->do_upload('post_image'); $up_file_name = $this->upload->data(); $post_image=$up_file_name['file_name']; $created_date=date('Y-m-d h:i:s'); $update_date=date('Y-m-d h:i:s'); $data=array('post_title'=>$this->input->post('post_title'),'url_slug'=>$url_slug,'post_category'=>$this->input->post('post_category'),'post_image'=>$post_image,'post_description'=>$this->input->post('post_description'),'created_date'=>$created_date,'update_date'=>$update_date); $this->PostModel->InsertBlog($data); } ?>
Generate URL slug (Controller)
<?php function generate_url_slug($string,$table,$field='url_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; } ?>
Insert POST (Model)
<?php function InsertBlog($data){ $ins_qry=$this->db->insert('blog',$data); if($ins_qry){ $this->session->set_flashdata("success","Success! Blog create successful!"); return redirect('page url'); }else{ $this->session->set_flashdata("error","Error! Blog create faild!"); return redirect('page url'); } } ?>
If you wish for to obtain a good deal from this piece of writing then you have to apply such methods to your won web site.
Hello. And Bye.
Hi, Thanks for comment here.
I consider Google is most utilized search engine.