CodeIgniter Forums
Pass ID, display title in URL - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Pass ID, display title in URL (/showthread.php?tid=33785)



Pass ID, display title in URL - El Forum - 09-08-2010

[eluser]Kamy[/eluser]
Hi:

I'm new to CI, so this question might have been answered already.
I searched the forum before posting.

Basically what I need to do is after passing the articleID to retrieve the info, instead of a url like: http://www.mysite.com/article/123

I'd like the url to be http://www.mysite.com/article/article-title

I've already setup all the code that passed the id and displays data properly.

please help

thanks

Kamy


Pass ID, display title in URL - El Forum - 09-08-2010

[eluser]johnpeace[/eluser]
So...

All you need to do is rewrite your select query so that it selects the article by title instead of by ID....then pass the title in the URL, right?

It might take a little string manipulation to make sure the titles are URL safe, but that's easy.


Pass ID, display title in URL - El Forum - 09-08-2010

[eluser]Kamy[/eluser]
passing title in the query is not a good practice, as it includes utf chars and spaces


Pass ID, display title in URL - El Forum - 09-08-2010

[eluser]smilie[/eluser]
Kamy, so what you are looking for is to see title in the URL, but to search based on the ID.
I do not think this will be easy.

CI considers everything behind controller name to be variable to be used in the controller.
Only thing that comes to my mind is following:

When calling controller, always to use forms. Meaning:
Page A -> index page (for example).
From there user clicks on a link which takes him to:
http://www.mysite.com/article/article-title
'Underwater' link is part of a form, with hidden article ID field.

In the controller, you simply 'ignore' article title variable and work with $this->input->post('articleid').

This way, user will see article title in URL, but all code will be done through article ID.

Maybe there are other ways, but I do not know them.

Hope this helps.

Regards,
Smilie


Pass ID, display title in URL - El Forum - 09-08-2010

[eluser]CroNiX[/eluser]
You can always pass both. I've noticed vBulletin is now doing this for SEO purposes. Just split on '-' and grab first value to get ID.

Example URL using 92 as the index:
http://www.vbulletin.com/forum/forumdisplay.php?92-vBulletin-Impex-Import-System


Pass ID, display title in URL - El Forum - 09-08-2010

[eluser]CroNiX[/eluser]
Another thing I do sometimes is to have the ID in a hidden form variable which gets passed and use the URL for URL friendly things. Obviously won't work for pages that don't use forms.


Pass ID, display title in URL - El Forum - 09-08-2010

[eluser]Kamy[/eluser]
Looking at CI docs, it seems url_title() is the way to go.
However I don't have any experience with that.

I give it a go, any ideas?