Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Ady

Ady Avatar

**
Official Member

90


August 2005
<?php

switch($_GET[page]) {

default:
echo 'This is the homepage.';
break;

case 'downloads':
echo 'This is the downloads page, you can get to it with ?page=downloads';
break;

case 'contact':
echo 'This is the contact page, you can get to it with ?page=contact';
break;

};

?>


Explanation

switch($_GET[page]) {
This starts the switch. In this case, the switch is dependant on the $page variable that is found in the URL.

default:
echo 'This is the homepage.';
break;

This piece of code sets the default page. If the $page variable in the URL doesn't equal any of the cases, it'll show the default page. The "break;" is there to end that case.

case 'downloads':
echo 'This is the downloads page, you can get to it with ?page=downloads';
break;

This bit will make a new case called "downloads", so if the variable $page equals "downloads", it'll show this page. Again it has the "break;" at the end to end the case.

case 'contact':
echo 'This is the contact page, you can get to it with ?page=contact';
break;

Basically the same as above.

};
End the switch :)

You can also have switches inside switches, like this:

<?php

switch($_GET[page]) {

default:
echo 'This is the homepage.';
break;

case 'downloads':

switch($_GET[category]) {

default:
echo 'Default category listings';
break;

case 'audio':
echo 'Audio downloads, accessed with ?page=downloads&category=audio';
break;

case 'games':
echo 'Game downloads, accessed with ?page=downloads&category=games';
break;

};

break;

case 'contact':
echo 'This is the contact page, you can get to it with ?page=contact';
break;

};

?>


Hope you learnt something :)


Last Edit: Mar 21, 2007 18:44:04 GMT by Ady

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
I used to use a similar method to this, but now I prefer the in_array and include method. Works best for me on larger projects, though this works fine on smaller ones.

splatcatball99

splatcatball99 Avatar

*
New Member

4


May 2007
so my site would essentiall be one file? Or does chris mean he uses like <?php
include_once('index.php');
?>


for the case?


Last Edit: May 22, 2007 23:37:02 GMT by splatcatball99

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
splatcatball99 said:
so my site would essentiall be one file? Or does chris mean he uses like <?php
include_once('index.php');
?>


for the case?


You give the appearance of it all being one file. This might be your entire index.php file:

<?php
switch($_GET[page]) {

default:
echo 'This is the homepage.';
break;

case 'downloads':
echo 'This is the downloads page, you can get to it with ?page=downloads';
break;

case 'contact':
echo 'This is the contact page, you can get to it with ?page=contact';
break;

};
?>


blu

blu Avatar
My Person Text...

**
Official Member

25


January 2007
Wow, it was a very small tutorial, but you can bet that I learned a whole lot of things from it.

newBookmarkLockedFalling