Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Changing shirt.php URL to SEO friendly URL

I just finished up the "Cleaning URLs with Subfolders" lesson in the Enhancing a Simple PHP Application course. Everything's working great but the URLs for shirt detail pages currently use product IDs. I'm curious if it's possible to use shirt names rather than IDs in the URLs? If so, where/how would I make those changes in the code?

Stephen Little
Stephen Little
8,312 Points

Not sure what our trying to do, giving shirt ID's is best. When you get into database you want to use ID's to access them not names. The answer is yes you could use name, in the product.php file that has the the function named get_list_view_html you can just change the displayed link to have $product['name'] but you would have to change all the shirts in the array to have names instead of numbers... It's late and I do not believe I explained this well...

Hope it help you out some :( Stephen

2 Answers

This is very common - you might see this type of thing being referred to as a 'slug'. It's essentially using a unique string instead of a integer to grab the correct data from your database. It exists as a column in your database - just like ID, Name or Description, you'll need a column for 'slug'.

You can see it in action right here on the Treehouse forums - take a look at the url! You have to make sure a slug is unique. For example on Treehouse, If you create a question with the same name as one that already exists, the code will add incrementing numbers to the end of the url friendly question name (slug) to keep it unique. It will still have a unique integer ID in the database, and it's highly likely database joins etc will be using the ID, not the slug.

It might go a little something like this:

  1. Validate product name.
  2. Create slug from product name.
  3. Compare new slug to existing slugs.
  4. If it's already in use, add a number to the end.
  5. Do steps 3 and 4 until you have a unique slug.
  6. Commit everything to the database.

This wouldn't be a great way because if you have 9 products e.g. pots-and-pans-1, pots-and-pans-2, pots-and-pans-3 etc... you would have to run 9 database queries until you hit a unique slug. It would probably be better to do one query where you search for all slugs LIKE the new slug name.. and then order the results by the date they were created or chop off the final number and order by that. Anyway, I digress - if that doesn't make sense to you now, it hopefully will after trial and error! I think you'll use the LIKE statement in future videos on this course.

It is quite a fun exercise to write a small class or function that will turn a product title or question title into a URL friendly slug - think about characters that you'll have to replace and what they should be replaced with. Having said that, packages are already in existence.

Top package search results:

(Be aware that I haven't tested these or read reviews - therefore they may be unsecure. Check this out before using them on any projects!)

If you're unfamiliar with packages and composer, Treehouse have just released some videos that teach you what they are and how they're use it. It's Badge Number Three - Auto loading and Composer.

Function

Hope this points you in the right direction!

Thanks guys!! I really appreciate it :)