Top

Includes using switch statment


PHP Code
+ 0 likes
Please Register to submit score.
Bookmark and Share
Average Score  4.0 (of 3 scores)
Date Added  May 04, 2007
Last Updated  May 04, 2007
Tags  id  include  switch 

Introduction

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.
<a href="index.php?id=news">Home</a>
<a href="index.php?id=about">About</a>

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.


Grab the Code

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

Comments

  (3)  RSS
Hawkee
Comments: 1,039
 
PHP Snippet:  Includes using switch statment
Posted on May 28, 2007 2:34 pm
Used in conjunction with header, footer and navigation includes this can be handy.
Noutrious
Comments: 365
 
PHP Snippet:  Includes using switch statment
Posted on May 28, 2007 2:50 pm
For those, who doesnt know,
Code:
<?php include('inc/head.php'); ?>
<?

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

   case "about":
      include "about.php";
   break;
}
?>
<?php include('inc/tail.php'); ?>
Blank
Comments: 6
 
PHP Snippet:  Includes using switch statment
Posted on Jul 7, 2007 5:00 am
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.

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom