Hrvatski

printer friendly version

Dynamic inclusion in PHP

This tutorial will teach you how to make updating layout and changing links a lot easier, and your links to be generated in the form index.php?page=mypage, using the include(); function.

If you're not familiar with the progamming language PHP, I suggest you to read the Introduction to PHP before you continue.

Step 1: Make separate files

make empty files named index.php, page1.php, page2.php, page3.php. Now add content.
index.php - this file contains only the layout. The content space is empty. Remember, this is an actual html file, which means it must have the basic HTML structure (<html> <head> </head> <body> </body> </html>) and what comes in between.
page1.php, page2.php, page3.php - these 3 files contain only the content. No layout. Just throw on some headings, paragraphs of text and images here.

Step 2: Include content

To the place provided for content content in index.php, copy the following code:

<?php
	$page = $_GET['page'];	/* gets the variable $page */
	if (!empty($page)) {
		include($page);
	} 	/* if $page has a value, include it */
	else {
		include('page1.php');
	} 	/* otherwise, include the default page */
?>

You can name $page anything you want - $x, $y, $go...
Since obviously you didn't set the value for $page in any way, opening index.php will always display page1.php. How to change this?

Make links: page 1, page 2, page 3. Each of them should state this:

<a href="index.php?page=page1.php">page 1</a>

Ofcourse, change the nuber 1 to 2 or 3 as needed.
What does this do? This link opens the page index.php, and sets the variable $page to value 'page1.php'. So, the code we wrote before will check if variable $page is set - obviously, it is. Next, it will include the file defined by $page. Each time we click one of the links, the page index.php opens over again, but with different content, that depends on which link we clicked because $page changes with each click. Uh, I hope you got this right because I really can't explain it different.

Step 4: Making it look snazzy

The link index.php?page=page1.php doesn't look that great. If we could get rid of that .php on the tail it would be better. Well, it's easy, if we know how to connect strings in PHP. And we do. (Have you read the Introduction to PHP?)

Change all links to <a href="index.php?page=page1">page 1</a> (delete the .php at the end of the link). Then add the following line to the code before the include function:

$page .= '.php';

The line above is short for $page = $page . '.php'; The entire code should then look like this:

<?php
	$page = $_GET['page'];
	if (!empty($page)) {
		$page .= '.php';
		include($page);
	}
	else {
		include('page1.php');
	}
?>

You're done! There is only something else we can do to make sure it includes only the files we want it to, a security patch if you want to call it so. We will create an array of strings $pages containing filenames that are allowed to be included. If the page defined by variable $page is not in the array, it will display the message: Page not found. Return to index.

<?php
	$page = $_GET['page'];
	$pages = array('page1', 'page2', 'page3');
	if (!empty($page)) {
		if(in_array($page,$pages)) {
			$page .= '.php';
			include($page);
		}
		else {
		echo 'Page not found. Return to
		<a href="index.php">index</a>';
		}
	}
	else {
		include('page1.php');
	}
?>

So, when you add more pages to your website, you will also need to add them to the array $pages to make it work :)
What's great about this method is that any time you decide to change the layout, you will only need to change the file index.php All other files remain exactly the same! :D

Including files from different folders

Someone asked me if this was possible. It is, and it's not very different from what you learned earlier.

If you have a folder named /info and inside it a page called faq.php, you can include it in the index.php file located in the root folder. The link would look like this:

<a href="index.php?category=info&page=faq">FAQ</a>

Including the file would look like this:

<?php
	$url = '';
	if (!empty($_GET['category'])) {
		$url .= $_GET['category'] . '/';
	}
	if (!empty($_GET['page'])) {
		$url .= $_GET['page'] . '.php';
	}
	include $url;
?>

The above example works in cases when category (folder) is defined, and it works when it's not defined as well.

Hope this tutorial was helpful. Good luck!


Share this tutorial with your friends!





My new book is out!

The Human Centered Brand by Nela Dunato


Branding & web design by Nela Dunato

Facebook