• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Permalink / Clean URL question

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

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

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

#4
[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!

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

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

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.