CodeIgniter Forums
Permalink / Clean URL question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Permalink / Clean URL question (/thread-9073.html)



Permalink / Clean URL question - El Forum - 06-10-2008

[eluser]little brittle[/eluser]
I'm setting up my blog so that I have "mysite.com/clean-article-title" directing to the correct article. When inserting the article info into the database, is there an easy way to check for duplicates and append "-2" or whatever number to the end of the newly inserted permalink title? What is the best practice for keeping the permalink unique?


Permalink / Clean URL question - El Forum - 06-10-2008

[eluser]Michael Wales[/eluser]
In your form processor use a callback to validate the slug, query the database for uniqueness, and append as necessary. Then perform your insert.

I would throw all of this into the model or a validator file if you use/have developed a library to support that.


Permalink / Clean URL question - El Forum - 06-11-2008

[eluser]little brittle[/eluser]
I figured something like that would work. I was just wondering if there was some sort of auto increment value to append to unique columns in MySQL to reduce the number of queries and PHP processing necessary. My blog is going to have some articles created automatically from various content sources, so duplicate permalink titles is going to be an issue and I wanted to make sure I was handling it the right way. Thanks for the help.


Permalink / Clean URL question - El Forum - 06-11-2008

[eluser]Gavin Blair[/eluser]
Your articles can have a title ("My Article Title") and a nicename, like Wordpress does ("myarticletitle") It's just the title without caps or spaces. You could convert spaces and other characters to hyphens (-) too.

When creating the nicename, you'll want to check for duplicates so you can add the -2 if needed.

Code:
$this->db->select('nicename');
$this->db->where('nicename', $newnicename);
$query = $this->db->get('articles');
$duplicates = $query->num_rows();
if ($duplicates > 0) {
$duplicates += 1;
$newnicename = $newnicename . "-" . $duplicates;
}
$this->db->insert('articles', array( 'nicename' => $newnicename, 'title' => $newtitle, ... ));
Hope this helps!


Permalink / Clean URL question - El Forum - 06-11-2008

[eluser]Gavin Blair[/eluser]
darn, you guys are too fast for me


Permalink / Clean URL question - El Forum - 06-11-2008

[eluser]Spockz[/eluser]
What I do is this:

/blog/blog-id/blog-title.

With an redirect if the blog-id was right but the blog-title missing with status 301. So if your id's are unique, your links will be unique.


Permalink / Clean URL question - El Forum - 06-11-2008

[eluser]little brittle[/eluser]
Thanks for the help everyone, I think I have it figured out.