Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_GET with Switch Statement

Tags:

php

i have alot of get values that define the page the user gonna see , for example for "profile" i will show the profile page and so on..

to find out what page to display i tried to do something like that:

switch ($_GET) {
    case 'profile':
        require_once('function/profile.php');
        break;
    case 'team':
        require_once('function/team.php');
        break;

but it shows no result..

i send the GET request like that : index.php?profile , for example..

what is the problem here and how can i can manage to do something similar that work as well. thank you in advance!

like image 809
homerun Avatar asked Apr 19 '12 23:04

homerun


People also ask

Does PHP have a switch statement?

The PHP switch Statement Use the switch statement to select one of many blocks of code to be executed.

Can we give expression in switch case?

1) The expression used in switch must be integral type ( int, char and enum). Any other type of expression is not allowed.

Can switch statements use variables?

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

What will happen when a match is found in a switch conditional statement?

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed.


2 Answers

To make your example work, you could replace $_GET with key($_GET)

be aware though that key() will return the first key of an array, so if you change your URL's variable order, this line'll stop functioning.

like image 131
stewe Avatar answered Oct 06 '22 18:10

stewe


$_GET is an array from the key value pairs found in the query string (part of the url after the script name and a question mark).

For example, the query string test=1&foo=bar will translate to:

Array(
    test => 1
    foo => 'bar'
)

In the OP example, index.php?profile, you will end up with a $_GET array like:

Array(
    profile => null
)

Problem with doing urls like this is that it is non-standard. When you do things in a non-standard way, you have to come up with non-standard solutions to fix the problems.

Here are a few options along with issues that each has:

  1. You can use $_SERVER['QUERY_STRING'] which will get you everything after the ? in the url. This is fine if the only thing passed in the url is just profile (or some other single value). In that case, $_SERVER['QUERY_STRING'] will have nothing but profile in it. But you also then lose the ability to pass additional parameters in the get string.

  2. You can go with the method described by @stewe. The php key function will return the key from the current position in the array passed in. If you haven't done any looping, the current position is the first element. This will work fine with multiple get parameters as well. Your query string will just look like index.php?profile&test=1&foo=bar. The problem is that profile (or whatever page) has to be the first or else key will return whatever the key is for the first parameter passed.

  3. Another option is to just go with the standard method of using a key and value. Regardless of page, you use the same key and just the value changes. You then have urls that look like index.php?page=profile and you can always access the page using $_GET['page'].

  4. You can use mod_rewrite. It is simple to setup, most hosts support it (or some other similar) and there are millions of tutorials and examples on how to get it to work. You end up with the cleanest urls and it works with query string parameters. For example, /profile/ can be rewritten to point to /index.php?page=profile. The user sees /profile/ and php sees the standard. This allows you to use $_GET['page'] to get the requested page and not have to do extra parsing to get other values inside php.

like image 23
Jonathan Kuhn Avatar answered Oct 06 '22 18:10

Jonathan Kuhn