Adding a number to a "pretty url"

Hello everyone, I’m trying to build a blog using php - And I’m generating the URL’s for the posts, out from the titles… But the problem is, that if 2 posts have the same title - the link will be the same too…

This is my code:

[

 //Functions.inc.php file function makeUrl($title) { $patterns = array( - Pastebin.com](http://pastebin.com/r0D1fwMY) - 

That I want, is that each time I create a post it should be etc.

Title-of-post-1
Hello-world-2.. So it's adding a number to the url

domain.com/blog/title-number/ something like that.

Can any give me some tips how do do that?

Best regards,
Lucas

Well, you’d probably want to create a URL hash for each post based on the post’s title and save that in the database. Then, each time you go to create a new post you’d generate a URL hash, and then check the db to see if that hash already exists. If it does you’d have to check if there’s an integer on the end and, if there is, see what that integer is and then increment by one.

You can see how messy and confusing this will get.

I would just generate a unique hash for each post and do it as http://domain.com/blog/8UjeL3/post-title, where 8UjeL3 is the unique hash for that URL. This way you won’t run into any problems and each URL is unique regardless of the post’s title.

Make the URL hackable too, so that if a visitor enters http://domain.com/blog/8UjeL3 they will still be able to access the post.

Or…, you could you the date.

2011/12/25/blog-post-title.

Failing that, add a unique constraint for the blog title and capture the duplicate error. When this error occurs, keep tacking a number on (within a sensible range) until it is inserted.

I’d prefer to use date myself, or an auto inc id column and use the id in the blog title slug.

I had originally thought of the date too, and this would work well for a personal blog. However, if this is a system that will allow users to post he could still run into a problem if two users post the same title on the same day. :slight_smile:

Both things sounds like a great Idea, and the first example - with the unique hash was nearly what I would do - that I would do, is like here on sitepoint forums, It’s adding a int after the title, whatever the title already exists or not.

But the problem is - to get that small increasing number added to the url :confused:

Do like Anthony suggested and just append the ID number to the URL.

I’m just wondering how to do, because First I check if every field is filled out, when creating a post, then I make a url via the function, from the title-field, - But that function somehow should include, also to take the ID from the post, and addding it - But that I’m wondering - How should it get an ID, from a post that isn’t written to the database?

For me php is still a little unlogic sometimes, still a beginner :smiley: But trying hard.

I’m going to use it for myself only, so properly I wouldn’t write the same title twice, but now when I’m trying to make it, I could still prepare it, for getting the same title twice.

Ty

You can’t. You would need to INSERT the new post and then use mysql_insert_id() to get the last ID. If you wanted to store that in the db then you would need to perform an UPDATE.

If you want to do this all in one shot in the INSERT you should use a unique hash or the date method.

EDIT: You’ve probably noticed that a lot of websites use unique hashes or the date. They do this for a reason. :slight_smile:

Hmm, I think I will go for the unique hash then, thank you Tgavin

Bear in mind though, you’re still going to have collision issues with a “unique hash” only less frequently - more so if the hash is short(ened).

That’s a good point. You’ll want to make the hash field UNIQUE, regardless of which method you use, and check if your new hash exists first.

This sounds hard for a newb like me lol, I’ll look into it n’ see if I can get it working