Edmonton Web Design and Photography

Php Case statement


Posted in: Tutorials, Wordpress on December 8th, 2005

First I have to explain a bit of how this works. We use what’s called a URL query string, You have probably scene something like this “index.php?name=test” Were it says name is referring to the case statement. In any other example, if u were to put hello $name and use the URL file.php?name=James then the out put would be “hello James”?.

So in this case name will be the case you wish to communicate with and the page you wish to view is tutorials thus we use index.php?name=tutorials, in my case I use 2 one for content and one for sub-nav. To do this we use the & character to indicate that there is a second variable or case statement that we can’t to communicate with.

For our mission her we will only use one case statement.

We have home.php, page1.php, page2.php, and style.css. You can grab them here. Nothing special in here, but lets have a look at the code for our little site.

<?php
switch( $_GET['name'] )
{
case "page1": // page 1
include("page1.php");
break;
case "page2": // page 2
include("page2.php");
break;
default: // default page
include("home.php");
break;
}
?>

In the above code we see that we use the switch () function to change between our pages. The variable name can be anything you want as long as you remember to change your URL. If name doesn’t have a value then php will loop thro until it hits the default portion of the code. In this case we want it to be home.php so when you access your site with site.php you won’t get a error and you will receive the default content. If you want to see page 1 then we would use site.php?name=page1 and so on.

As you can see the site is nothing special, we have a table with the php code in it, cute and paste the above code into notepad and save it as site.php and put it with the in the zip. Upload it to your web server and test it out.You may be asking your self well great what good is this really. Well as you can see we have one page (site.php) and out content is in there own , every time you update your website you probly spend a lot of time editing your old code, this way when you update your design you have 1 file to change, and your content is all safe and already formatted. It will greatly speed up your productivity.

It’s the second best thing to a data base driven website. so have a look at the finished product.

Download Source File.
Downloaded 463 Times.

3 Responses to “ Php Case statement ”


  1. nice tutorial and yup i agree with you on the point how this increases the productivity.

    Thanks for the tutorial.

    rohit
    May 11, 2007

  2. Why thank you :)

    I will probably do a few tutorials on adding, editing, and removing content from MySQL, so check back for them!

    Adam Patterson
    May 11, 2007

  3. [...] want to have a current page tab you would use something like a case statement for navigation found here to navigate a site. php would then use the values from your navigation string and test to see if [...]


Search


Archive


Subscribe