Save this class file library generateslug.php
class GenerateSlug{
/*
* Developed by Manikanta
*/
public function get_result($colName, $tbName, $cond="",$return_column_value=0){
$ci = &get_instance();
$ci->db->select($colName);
if($cond!="")$ci->db->where($cond);
$data=$ci->db->get($tbName);
$res=$data->result();
$ci->db->last_query();
if($data->num_rows()>0){
if($return_column_value==1){
return $res[0]->option_value;
}
}
return $data->num_rows()>0 ? $res : FALSE;
}
function generate_slug($colname, $tablename, $value){
$newString = strtolower(preg_replace('/[^a-z0-9]/i', '_', $value));
if($this->get_result($colname,$tablename, array($colname=>$newString))==FALSE) return $newString;
return $this->generate_slug($colname, $tablename, $newString.rand(0,200));
}
}
//Calling the class in controller
$crud = new GenerateSlug();$post_array['slug']=$crud->generate_slug('slug','food_items',$post_array['name']);
print_r($post_array);
0 comments:
Post a Comment