Welcome Guest, Not a member yet? Register   Sign In
Permalink / Clean URL question

[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?

[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.

[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.

[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.

$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!

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

What I do is this:


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.

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

Theme © iAndrew 2016 - Forum software by © MyBB