Home › Forums › Back End › PHP passing requested post id and title in url but not showing id in the url
- This topic is empty.
-
AuthorPosts
-
August 8, 2013 at 11:52 am #146173
Jochim
ParticipantWhat’s the best way to achieve this? I’d like to have a forum with the post page displaying a url like
example.com/forums/category/post-title
. The problem is I want to look up the post in the database by it’s id instead of title because I’ve read that looking up by title is slow especially if you have a lot of posts.So to achieve this you would have to pass the title (or a slug of it) and the id, that’s what I currently have. I have my .htaccess set up to rewrite
example.com/forums/category/post-id/post-title
toexample.com/forums/post.php?id=post-id
. Now I just want to remove the id from the url, but I don’t know how… Any help please?Thanks.
August 8, 2013 at 2:20 pm #146181Jochim
Participant@traq Thanks for the reply and…
Depends on how your database table is indexed – if you have an index on the title -and every title is unique- then no, it’s not a problem.
I have my database set up like this:
post_id (just an AUTO INCREMENT), post_title (not unique), post_url (unique and slug of post_title and if the slug already exists add '-2, -3, ...')
but I don’t really know what you mean by ‘an index on the title’ ? So I guess I don’t have it :) And oh ok if matching the post_url doesn’t make any noticeable difference in speed then I could do that.
If your DB is not indexed this way, you can create an index for it. This would be the simplest solution.
What exactly is the index that the code creates? Could you explain that a little more please?
instead of writing example.com/forums/category/post-id/post-title, write example.com/forums/category/post-title?id=post-id instead.
Isn’t that almost the same except swich the
post_id
andpost_title
around?(Are you using WP (or some other CMS)?)
No I’m trying to make my own CMS, “it’s good practice” I told myself :) and I hope I can use it in a project once it’s finished..
August 11, 2013 at 9:29 am #146394Jochim
ParticipantSorry for the bump but it was needed :)
August 11, 2013 at 12:12 pm #146405Jochim
ParticipantYou need a unique value to search by. If the title is not unique, you run the risk of retrieving the wrong article.
Ok, so now I have it set up to look for
post_url
in the database, which is unique.. So to make an index for it I just press the Primary Key button next topost_url
in phpMyAdmin?August 11, 2013 at 1:51 pm #146423Jochim
ParticipantOk, so I created the index.. Do I need to change the code or do I just leave it to look for the post_url to find the page?
August 20, 2013 at 2:20 pm #147631Jochim
ParticipantOk thanks :)
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.