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 CRUD Operations with PHP Reading and Writing Reports Filtering by Time Period

Martin Park
Martin Park
12,792 Points

Could URL not be tidied up quite a bit here?

I'm currently looking at the URL produced by this as:

http://localhost/reports.php?filter=date%3A07%2F29%2F2018%3A08%2F04%2F2018

Would we not want that tidied up to be something like:

http://localhost/reports.php?filter=date_07_29_2018_to_08_04_2018

At least then the user can clearly see what they are actually looking at from the URL and sharing the URL also gives others the same foresight.

Or is there a reason this would not be done?

2 Answers

Niki Molnar
Niki Molnar
25,698 Points

It's a good question but URLs can only be sent over the Internet using the ASCII character-set.

Since URLs often contain characters outside the ASCII set (like underscores), the URL has to be converted/encoded into a valid ASCII format.

URL encoding replaces unsafe ASCII characters with a % and by two hexadecimal digits, which is called Percent Encoding and what you see in your URL. Do a search for ASCII character set in Google or the like, to find the list and limitations.

It's not part of the workshop but in the real world, you could pass the dates as 20180728+20180408 (the + will decode as a space and isn't usually encoded). This way of formatting dates is pretty standard and is always YYYYMMDD. In reality, though, you would POST the dates, rather than use GET and then display the dates that the user chose on the page, so they know what they searched for.

Hope that helps!

Niki

Martin Park
Martin Park
12,792 Points

Thanks so much for the reply and info Niki I appreciate it. You've actually helped me more than you realise.

So here a ":" has been turned into "%3A" and a "/" has been turned into "%2F" because they are not part of the 128 character ASCII set.

I wouldn't normally expect to see those characters in a URL so I do understand they have to be encoded.

Perhaps in my example above I should have used "-" instead of "_". As I see "-" in URLs all the time.

So with some string replacement the URL could be converted into something like: http://localhost/reports.php?filter=date-07292018-08042018. That would just look tidier to me :) And using UK dates because US dates look foreign to me lol

So "-" would work similarly to "+" too right since "-" is included in the ASCII set?

Niki Molnar
Niki Molnar
25,698 Points

(I'm in the UK too but the Internet has generalised dates to YYYY-MM-DD or YYYYMMDD so as not to annoy either side of the Atlantic!)

And yes, you're right - you can use a dash.

Niki