Includes using switch statment

By DeathfireD on May 04, 2007

Sick of ending up with hundreds of static html pages? Well includes can help. Here is an example of how you can make your index page content dynamic.

Step 1) Make your layout with all menus and links on it.

Step 2) Place the snippet code in the spot where you want your content to go, probably in the middle of the site.

Step 3) Open up a new fresh php file and add hello world to it. Save the file as news.php.

Step 4) Open a fresh new php file and add this is the about page. Save the file as about.php

Step 5) Change two of your links on the layout page to this.
Home
About

Step 6) Save the 2 files and visit http://yousite/index.php

PS. you can continue to add pages to the snippet below just be sure to add the urls to your layout page.

<?

switch($_GET['id'])
{
   default:
   case "news":
      include "news.php";
   break;

   case "about":
      include "about.php";
   break;
}
?>

Comments

Sign in to comment.
Blank   -  Jul 07, 2007

nice and simple. ;)
it took me a while to get into switch, only got into it because my \"if\" statements were extremely long and messy.

 Respond  
Hawkee   -  May 28, 2007

Used in conjunction with header, footer and navigation includes this can be handy.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.