Click on thumbnailed images to enlarge

PHP dynamic inclusions is a better alternative to using Iframes and also allows you to better organize and manage your website. It gives you a better way to change your content.

Before attempting to follow this tutorial, you must understand [url=http://www.createblog.com/forums/index.php?showtopic=82865]PHP includes[/url].

[size=10]A Case Example[/size]
Have you seen websites with URLs like the following:

http:// byroxanne.com/[color=Blue]index2.php[/color]?[color=Green]page=home[/color]

Browse [url=http://byroxanne.com/index2.php?page=home]my digital portfolio[/url] and take note of the URLs that appear in your address bar.

The content for each of these pages are being loaded from within the same page - [color=blue]index2.php[/color], similar to a page containing an iframe. The [color=Green]page=home[/color] portion of the URL denotes which page content to load and in this case, the [color=Green]home[/color] page. This is why we call it dynamic inclusion: we are passing a variable through the URL. If you wanted to view the [color=Green]resume[/color] page, the URL will be:

http:// byroxanne.com/[color=Blue]index2.php[/color]?[color=Green]page=resume[/color]

[size=10]The Code[/size]
We're going to start where we left off from the [url=http://www.createblog.com/forums/index.php?showtopic=82865]PHP Includes tutorial[/url]. Currently, you should have 3 files: [color=Red]header.php[/color], [color=Blue]footer.php[/color], and [color=Green]generic.php[/color]. Make sure you are uploading everything to the same directory or this will not work properly.

[color=Red]INDEX PAGE [/color]
Now copy and paste this code to a new file and save it as [color=green]index.php[/color], and upload:


[color=red]// Include header

[color=green]// Include content
if (is_file("$x.php")) {
else {
include("default.php"); // the default page

[color=blue]// Include footer

[color=Red]PAGE ONE [/color]
Create a new file, paste the following and save it as [color=green]page1.php[/color].

[quote]<h1>Page One</h1>

<p>Welcome to page one.</p>[/quote]
To view this page, upload the file and type index.php?[color=blue]x[/color]=[color=green]page1[/color]

[color=Red]PAGE TWO [/color]
Create another new file, paste the following and save it as [color=green]page2.php[/color].

[quote]<h1>Page Two</h1>

<p>Welcome to page two.</p>[/quote]
To view this page, upload the file and type index.php?[color=blue]x[/color]=[color=green]page2[/color]

[color=Red]DEFAULT PAGE [/color]
Create another new file, paste the following, and save it as [color=green]default.php[/color].

[quote]<h1>Default Page</h1>

<p>Welcome to my default page. This content will be displayed if no variable is passed through the URL or if the variable passed in the URL is invalid.
To view this page, upload the file and type index.php?[color=blue]x[/color]=[color=green]default[/color]

Now type index.php WITHOUT the "?[color=blue]x[/color]=[color=green]page[/color]" to see what happens when you don't pass a variable. Type index.php?[color=blue]x[/color]=[color=green]page3[/color] to see what happens when you try to view page three, which is invalid since we didn't create a file for the content of this page.

As you can see, the default page appears when there is no value or an invalid value to our variable, [color=blue]x[/color].

[color=Red]ADDITIONAL PAGES [/color]
Well, that's it. If you want to add more pages of content, just create new .php files. If you save it as [color=green]aboutme.php[/color], to access it you will have to type index.php?[color=Blue]x[/color]=[color=green]aboutme[/color]

Note that in your [color=green]index.php[/color] file which contains the header, footer, and content script:


// Include header

// Include content
if (is_file("$[color=Blue]x[/color].php")) {
else {
include("[color=Red]default[/color].php"); // the default page

// Include footer

You can replace [color=blue]x[/color] with any character or text of your choosing. If you do so, make sure you know the url has changed. Instead of index.php?[color=blue]x[/color]=page1 it'll end up being index.php?[color=blue]yourtext[/color]=page1.

You may also replace [color=red]default[/color] with whatever filename you wish to give your default page.

Your three pages should be the following: [url=http://xquizit.cliquemb.com/php/index.php]index.php[/url], [url=http://xquizit.cliquemb.com/php/index.php?x=page1]index.php?x=page1[/url], [url=http://xquizit.cliquemb.com/php/index.php?x=page2]index.php?x=page2[/url].

Hopefully you're beginning to see how coding your website in PHP will help you save time and is helpful in maintaining your website. To sum it up, PHP Includes makes changing your layout easier and PHP Dynamic Inclusion makes changing your content easier.


Copyright 2005 Xquizit
You are not authorized to copy or distribute any content from this tutorial

Tutorial Comments

Showing latest 1 of 1 comments

For some reason it's not working...?

By Elleusion on Feb 24, 2006 9:42 pm

Tutorial Details

Author xquizit View profile
Submitted on Feb 11, 2006
Page views 19,590
Favorites 0
Comments 1