Tuesday, 9 October 2007

Load pages using URL variables and PHP include() function

In the previous post we have seen the conceptual design for index.php page. This page can be defined the "container" of our site. Inside the #main section we will load all the other pages, arranging the use of URL variables and PHP include() function. In this way, we will have a single page that define the global layout for our site (index.php: topbar, navigation bar, footer).
An URL variable is a variable visible from the user into the browser navigation bar identify from the char "?" + "varname". This is the typical approach using URL variable:



A PHP URL variable is identified from the following code:

$_GET['varname']


Imagine our site have only tree pages: Home, About me, Interests; we want to use URL variables to load the relative pages saved into include folder of our site root (take a look to the complete site structure post).


Copy and paste this code inside the #main section:

<div id="main">
<?php
//If is defined URL variable 'aboutme'
if(isset($_GET['aboutme'])){
// include page about me
include('include/in-aboutme.php');
//else if is defined URL variable 'interests'
}else if(isset($_GET['interests'])){
// include page interests
include('include/in-interest.php');
// in all other cases include the home page
} else {
include('include/in-home.php');
}
?>
</div>


In this way, when is defined an URL variable that satisfies some criteria (in this case the name), the PHP code load the relative page. In all other cases, or if is not defined a URL variable, load the default page in-home.php (our home page).


In the next lesson I will explain an example of a typical navigation bar that use link with URL variable to navigate on your site.

No comments:

Post a Comment