Web Development
Using PHP

Simpson College
Computer Science

Creating a Menu

This is an example of two pages in a website. The user may navigate between the two. Note that there is a lot of similar code between the pages.

Version 1

<!DOCTYPE HTML>

<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>My Menu Test Title</title>
</head>

<body>
  <a href='menu1.php'>Page 1</a> <a href='menu2.php'>Page 2</a>

  <p>The first page of my site.</p>

  <p>Page 1</p>
</body>
</html>
<!DOCTYPE HTML>

<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>My Menu Test Title</title>
</head>

<body>
  <a href='menu1.php'>Page 1</a> <a href='menu2.php'>Page 2</a>

  <p>This is the second page of my site.</p>

  <p>Page 2</p>
</body>
</html>

Version 2

The common HTML code has been moved to a couple separate PHP files. These files are brought in with an include function. This prevents duplication and allows multiple pages to be updated simultaneously.

It is possible that a person could directly navigate to the common include files. This is an issue that can be fixed, but that solution is beyond the scope of this chapter.

<!DOCTYPE HTML>

<html lang="en">

<?php
include("common_head_section.php");
?>

<body>
<?php
   include("common_menu.php");
?>

  <p>The first page of my site.</p>

  <p>Page 1</p>
</body>
</html>
<!DOCTYPE HTML>

<html lang="en">

<?php
include("common_head_section.php");
?>

<body>
  <?php include("common_menu.php");  ?>

  <p>This is the second page of my site.</p>

  <p>Page 2</p>
</body>
</html>
<head>
 <title>My Menu Test Title</title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<?php
	echo "<h1>My Menu Test</h1>";
	echo "<p>";
	echo "<a href='menu1.php'>Page 1</a> ";
	echo "<a href='menu2.php'>Page 2</a>";
	echo "</p>";
?>

Version 3

In this version, the concepts of functions, and if statements have been introduced. The file menu2.php calls the date function to get the current day of the week. It then uses if statements to trigger output that will automatically change depending on what day of the week it is.

A function is created in common_menu.php and called in the menu files to write out the menu. By using if statements, this version of the menu system prevents links from being created in the menu that link back to the page the user is already on.

<!DOCTYPE HTML>

<html lang="en">
<?php
include("common_head_section.php");
?>

<body>
  <?php
  include("common_menu.php");
  writeMenu('menu1.php');
  ?>

  <p>The first page of my site.</p>

  <p>Page 1</p>
</body>
</html>
<!DOCTYPE HTML>

<html lang="en">
<?php
include("common_head_section.php");
?>

<body>
  <?php
  include("common_menu.php");
  writeMenu('menu2.php');

  ?>

  <p>This is the second page of my site.</p><?php
  $d=date("D");
  echo "The current day is $d.";
  if ($d=="Fri") {
          echo "Nooo! Please don't sing Katy Perry!";
  }
  if ($d=="Mon") {
          echo "Ugh. Monday.";
  }
  if ($d=="Mon" || $d=="Wed" || $d=="Fri" ) {
          echo "<p>CIS 305 day";
  }
  ?>

  <p>Page 2</p>
</body>
</html>
<head>
 <title>My Menu Test Title</title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<?php
function writeMenu($current_page) {
	echo "<h1>My Menu Test</h1>";
	echo "<p>";
	if( $current_page == "menu1.php" ) {
		echo "Page 1 ";
	} else {
		echo "<a href='menu1.php'>Page 1</a> ";
	}
	if( $current_page == "menu2.php" ) {
		echo "Page 2";
	} else {
		echo "<a href='menu2.php'>Page 2</a>";
	}
	echo "</p>";
}
?>

Version 4

This version shows how to use arrays and for statements to have several menu items without creating a long chain of if statements.

<!DOCTYPE HTML>

<html lang="en">
<?php
include("common_head_section.php");
?>
<body>
<?php
include("common_menu.php");
writeMenu('menu1.php');

?>
<p>The first page of my site.</p>
<p>Page 1</p>
</body>
</html>
<!DOCTYPE HTML>

<html lang="en">
<?php
include("common_head_section.php");
?>
<body>
<?php
include("common_menu.php");
writeMenu('menu2.php');

?>
<p>This is the second page of my site.</p>
<?php
$d=date("D");
echo "The current day is $d.";
if ($d=="Fri") {
	echo "Nooo! Please don't sing Katy Perry!";
}
if ($d=="Mon") {
	echo "Ugh. Monday.";
}
if ($d=="Mon" || $d=="Wed" || $d=="Fri" ) {
	echo "<p>CIS 305 day";
}
?>
<p>Page 2</p>
</body>
</html>
<head>
 <title>My Menu Test Title</title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<?php
function writeMenu($current_page) {
	echo "<h1>My Menu Test</h1>";
	echo "<p>";
	$links=array("menu1.php","menu2.php");
	$titles=array("Page 1","Page 2");
	
	for( $i=0; $i < count($links); $i++ ) {
		if ($links[$i] != $current_page ) {
			echo "<a href='$links[$i]'>$titles[$i]</a> ";
		} else {
			echo "$titles[$i] ";
		}
	}
	

	echo "</p>";
}
?>

You are not logged in. Log in here and track your progress.