PHP Navigation using Switch

By: Jacob Manser (lefteh) Posted: 27-Dec-05, 22:38:01 Rated: General/Experienced

Alert!: Warning: This article/tutorial is more the 45 days old. As such the information contained within could be, by now, out of date. Please read all information to make sure that this article/tutorial will work with your current version of the Operating System

In this tutorial, you will learn how to use PHP’s “switch” capability to create your site’s navigation. The switch is basically an alternative to a million ifs or elseifs.

First of all, decide what GET variable your navigation will be based upon. For example, this tutorial will use $_GET[‘page’]. Therefore, our links will be like this: <a href=”?page=about”>.

Now, let’s learn the basic outline of a switch.


<?php
switch ($var) {

case “page1”:

break;

case “page2”:

break;

case “page3”:

break;

default:

break;

}

?>

Every time the “case” statement is used, the code is testing the value of the variable that is the argument of switch().

For example, in the above example it says “switch($var)”.
This means that the variable that will be tested is $var.
When it says case “page1”, it is seeing if the value of $var is “page1”. If it is, then it executes the code between the colon ( : ) and the break; statement.

The default: part acts like a last resort, like an else.

Now, let’s put this to use. Let’s say I have four pages on my site: Home, About, Products, and Contact. I will use $_GET[‘page’] as my switch variable, and make the links as follows:


<a href=”?”>Home</a>
<a href=”?page=about”>About</a>
<a href=”?page=products”>Products</a>
<a href=”?page=contact”>Contact</a>

The PHP code will be as follows:


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

case “about”:
echo “About”;
break;

case “products”:
echo “Products”;
break;

case “contact”:
echo “Contact”;
break;

default:
echo “Home”;
break;

}

?>

 

Create an easy PHP Navigation with Query Strings

This system of doing things is pretty simple. First lets go over the breakdown of what exactly this script will do. Basically it will create an easy way to navigate your site that looks quite neat. EX. http://www.watever.com/index.php?id=tutorials. In the example the page being shown will be the tutorials page. Lets take a look at the code first, then we can run through a quick breakdown.

<?php
$id = $_GET[“id”];
$id = addslashes(strip_tags($id));

switch( $id )
{
default:
include ‘home.php’;
break;

case ‘home’ :
include ‘home.php’;
break;
}
?>

Some people use IF statements, but this way seems a little neater.

Line 1: Initiates the PHP file. Nothing really happens here.
Line 2: This is the opening of the switch statement. Basically it “GET”s the id from the address.
Line 4-6: Is the default switch statement. If there is no id then it will include the file home.php
Line 8-10: Is the first case. It says, IF the id is home THEN include home.php.

To add more links you just need to add more of the following:

case ‘contact’ :
include ‘contact.php’;
break;

Don’t forget to change the case and the include file.

Implementing The Code
Implementing the code is pretty simple as well. Basically you build your main page, and where you want your main content to appear you put in this SWITCH statement. Then for each file, (EX. home.php) you only put the content. Hers a quick example of what a page would look like:

<?php
include’dbconnect.inc.php’;
?>
<html>
<head>
<title>Xangelo</title>
<link rel=”stylesheet” type=”text/css” href=”style.css”>
</head>
<body>
<div class=”header”>
Navigation
</div>
<a href=”index.php?id=home”>Home</a>
<div class=”content”>
<?php include’id.php’; ?>
</div>
</body>
</html>

And you home.php file would look like this:

Welcome to my site

The file id.php contains your switch statement. If you need more help implementing this, feel free to drop me an email.

*Edit*
Made page slightly more secure with use of addslashes/strip_tags code performed on variable.

And that’s all folks. You can add as many cases as you need, and do anything you want in the meat of the cases.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *