I’m trying to get the last variable name displayed in my url and use it to determine what’s displayed on my page. For example, here’s my url:
http://mysite.php/catalog/?department=123&category=456&sub_category=789
If the URL is showing a sub_category then I want to display everything that would be considered a sub category. But, if the url only displays:
http://mysite.php/catalog/?department=123&category=456
Then I want to display content that only pertains to category.
Once I get the last varibale name I’ll use a switch statement to display the content. I’ve been working with this code, but it’s only getting the first variable name and not the last?
switch(key($_GET))
{
case 'department':
print('show department');
break;
case 'category':
print('show category');
break;
case 'sub_category':
print('show sub_category');
break;
}
Maybe this isn’t the best approach, and if you have a better way than using a switch statement let me know 😉
Do not rely on the order of query string parameters. The better approach would be to check for the presence of parameters in a specific order: