CodeIgniter Forums
unique url (slug) and check - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: unique url (slug) and check (/showthread.php?tid=16421)



unique url (slug) and check - El Forum - 03-06-2009

[eluser]freshface[/eluser]
Hey

I am creating a slug for my url title but i want to check if it is unque in my database.

when i have: ci-rocks and i post another title with ci-rocks it should become ci-rocks-1 but if i post another time ci-rocks it should become ci-rocks-2 and so on.

But i have a proplem with my recusion in my model.

Code:
var $counter = 0;
    var $orig_slug = '';
    function unique_slug($slug)
    {
        if($this->orig_slug == '')
        {
            $this->orig_slug = $slug;
        }
        
        $this->db->where('slug', $this->orig_slug);
        $this->db->from('blog_blog');
        $count =  $this->db->count_all_results();
        
        if((int)$count != 0)
        {
            $this->counter++;
            $slug = $this->orig_slug . '-' . $this->counter;
            $resp = $this->unique_slug($slug);
            
            
            return $slug;
            
            //var_dump($this->unique_slug($slug));
            
            //return $slug;
            
            //if(!$this->unique_slug($slug))
            //{
            //    //$this->unique_slug($slug);
            //    
            //}
            //else
            //{
            //    return $slug;
            //}
            //
            //return FALSE;
        }
        else
        {
            return $this->orig_slug;
        }

Does somebody know my mistake?


unique url (slug) and check - El Forum - 03-06-2009

[eluser]xwero[/eluser]
Why make it so difficult?
Code:
function unique_slug($slug)
{
   // if a similar slug is found with a postfix a slug with a higher postfix needs to be returned  
   $query = $this->db->select('MAX(slug) slug')->like('slug',$slug,'after')->get('blog_blog');
  
   if($query->num_rows > 0)
   {
      $prev_max = $query->row()->slug;
      $new_max = $slug . '-' . (str_replace($slug . '-','',$prev_max)+1);
      return $new_max;
   }
   // if the same slug is found the slug is the first with a postfix
   $this->db->where('slug', $slug);
   $this->db->where_not('slug', $slug.'-2');
   $this->db->from('blog_blog');
   $count =  $this->db->count_all_results();

   if($count == 1)
   {
      return $slug.'-2';
   }
   // if the two checks above fall through the slug in the parameter is unique
   return $slug;
}



unique url (slug) and check - El Forum - 03-06-2009

[eluser]freshface[/eluser]
Thx, i was playing around with your code and it seems that it always returns the slug + postfix.
When 'ci-rocks' is unique it creates 'ci-rocks-1'

The first if statement always seems to be true because $query->num_rows is always 1 even when there are no records in the db.

Am i correct?

Regards.


unique url (slug) and check - El Forum - 03-06-2009

[eluser]xwero[/eluser]
You are right, the solution is
Code:
$prev_max = $this->db->select_max('slug')->like('slug',$slug,'after')->get('blog_blog')->row()->slug;

if( ! is_null($prev_max))
{
//...



unique url (slug) and check - El Forum - 03-06-2009

[eluser]freshface[/eluser]
Works great, thx.