Overview:
Now that we have discussed the basic concepts of a CMS written in PHP, we should begin to think about ways to make those concepts more useful and more powerful. Basically, using only what we have discussed so far, your website would still need a file for each and every page you wanted to display to your users. What we will discuss now a method of only creating one template file and a script that will automatically change the content of the page based on the url used to get to the page. This would give you dynaically generated webpages.
Additionally, we will discuss ways of storing your data in a flat file database. In these databases, we will store information about your menu items and advertising banners.
Purpose:
While the use of a seperate menu script that included on every page in your website is very handy when it comes to making changes to your website menu, this is only one very small time saving tool for a web master. With the system discussed in CMS101 - Content Management System Design a full website overhaul would still require the webmaster to edit several file in the same way every time. The template of each page would need to be updated. This would take a lot of time to do and after the first 10 pages or so would get rather tedious. It would be much better to have a single template file that is used for the entire website. Then only the single template file would need to be updated when a change was desired.
Editing would be simplified even more if the majority of the content for the website was stored in an easy to read and logical system of data files. This would make finding the right data to edite much easier since it would be mostly a text file. I know there have been times where I've gotten lost in my code and edited the wrong portion of the file. This would reduce those user errors most of use have encountered. For the menu script, having a seperate database file for the menu items would allow you to enter only the most basic information needed to generate the menu. Then the need to remember how to properly use the menu generation function will be eliminated.
Technical:
We will first need to create a template for our website and code it to use different content based on the url used to access the page. We can use the template from the previous lesson as the basis for the new one. For now the only change will be for the main content area. To do this, we will add some code to the beginning of the file that will identify the correct content to be displayed in the main content area of the webpage then in the main content area of the template, we will insert that content.
In order to identify the correct content to display on the webpage, we will need to encode some data in the url for the webpage like so:
For this lesson, the url for the website will be www.mycms.com/index.php. This will be the front page or default page of the website. If we want a different page, we will need to pass a variable through the url. www.mycms.com/index.php?page=links should display a page with a list of links on it. However, www.mycms.com/index.php?page=news1 should display a page with some type of news article or articles on it. It is important to be able to display the correct conten all of the time and if an error occurs either display an error message or the default page instead of the error code that would be generated. While explaining to your user that either the data they requested could not be found or no longer exists is a very professional way of dealling with an error, allowing the user to view a generic server generated PHP code error instead is very unprofessional. To prevent this, we need to be sure that www.mycms.com/index.php?page=gjhgjhgkjg will either display the default page or a professional looking message that the content could not be found.
Here is how we would retrieve the desired content from the url provided and check to see if the content is valid.
CODE
<?php
$page= $_GET['page'];
$content_file = 'content/pages/'. $page . '.php';
// Now we check to see if that file exists
if (file_exists($content_file)) {
$main_content_file = $content_file;
}
else {
$main_content_file = 'content/pages/default.php';
}
?>
$page= $_GET['page'];
$content_file = 'content/pages/'. $page . '.php';
// Now we check to see if that file exists
if (file_exists($content_file)) {
$main_content_file = $content_file;
}
else {
$main_content_file = 'content/pages/default.php';
}
?>
We of course place that script at the beginning of the index.php file and where the content is to be displayed we insert <?php include($main_content_file); ?> like so:
index.php:
CODE
<?php
$page= $_GET['page'];
$content_file = 'content/pages/'. $page . '.php';
// Now we check to see if that file exists
if (file_exists($content_file)) {
$main_content_file = $content_file;
}
else {
$main_content_file = 'content/pages/default.php';
}
?>
<html>
<head>
<title>My CMS 2</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" style="font-family: verdana;">
<table width="100%">
<tr>
<td colspan="2" bgcolor=silver>
<?php include("header.php"); ?> <!-- Used for Banner Advertising etc... -->
</td>
</tr>
<tr>
<td width="150" bgcolor=red valign="top">
<?php include("menu.php"); ?> <!-- Used for The Main Menu... -->
</td>
<td bgcolor=navy>
<?php include($main_content_file); ?> <!-- Here is where our selected content will be displayed -->
</td>
</tr>
<tr>
<td colspan="2" bgcolor=purple>
<?php include("footer.php"); ?> <!-- Banner Ads, Copyright Info., etc... -->
</td>
</tr>
</table>
</body>
</html>
$page= $_GET['page'];
$content_file = 'content/pages/'. $page . '.php';
// Now we check to see if that file exists
if (file_exists($content_file)) {
$main_content_file = $content_file;
}
else {
$main_content_file = 'content/pages/default.php';
}
?>
<html>
<head>
<title>My CMS 2</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" style="font-family: verdana;">
<table width="100%">
<tr>
<td colspan="2" bgcolor=silver>
<?php include("header.php"); ?> <!-- Used for Banner Advertising etc... -->
</td>
</tr>
<tr>
<td width="150" bgcolor=red valign="top">
<?php include("menu.php"); ?> <!-- Used for The Main Menu... -->
</td>
<td bgcolor=navy>
<?php include($main_content_file); ?> <!-- Here is where our selected content will be displayed -->
</td>
</tr>
<tr>
<td colspan="2" bgcolor=purple>
<?php include("footer.php"); ?> <!-- Banner Ads, Copyright Info., etc... -->
</td>
</tr>
</table>
</body>
</html>
Now as long as as you create a file that matches the filename passed in the url, the content will be displayed.
Here is an example:
index.php?page=main --> content/pages/main.php
index.php?page=news --> content/pages/news.php
index.php?page=links --> content/pages/links.php
index.php?page=other --> content/pages/other.php
index.php?page=default --> content/pages/default.php
You could also add to the script to allow you to use the same system for other parts of your webpage.
you can pass multiple arguments in the url like so:
www.mycms.com/index.php?page=main&header=header1
With this you could also specify which header file to use. If you had a header file named header1.php, the contents of it could be inserted in the header while the contents of main.php would be inserted in the main content area.
To do this, you need to modify the script at the beginning of the index.php like so:
CODE
<?php
$page= $_GET['page'];
$header= $_GET['header'];
$content_file = 'content/pages/'. $page . '.php';
$header_file = 'content/headers/'. $header. '.php';
// First we check the content file and assign a value to it's variable
if (file_exists($content_file)) {
$main_content_file = $content_file;
}
else {
$main_content_file = 'content/pages/default.php';
}
// Next we check the header file and assign a value to it's variable
if (file_exists($header_file)) {
$main_header_file = $header_file;
}
else {
$main_header_file = 'content/headers/header_default.php';
}
?>
$page= $_GET['page'];
$header= $_GET['header'];
$content_file = 'content/pages/'. $page . '.php';
$header_file = 'content/headers/'. $header. '.php';
// First we check the content file and assign a value to it's variable
if (file_exists($content_file)) {
$main_content_file = $content_file;
}
else {
$main_content_file = 'content/pages/default.php';
}
// Next we check the header file and assign a value to it's variable
if (file_exists($header_file)) {
$main_header_file = $header_file;
}
else {
$main_header_file = 'content/headers/header_default.php';
}
?>
We then add <?php include($main_header_file); ?> to the place where we want the header to be displayed.
Our next goal is to use a flat file database as the source of the menu to be generated in menu.php.
A flat file database is simply a text file that uses some means of data seperation to store information. For our script we would create such a database in a new directory:
databases/menu.db
CODE
Search Engines|header
Alta Vista|www.altavista.com
Excite|www.excite.com
Google|www.google.com
Lycos|www.lycos.com
Yahoo|www.yahoo.com
Public Forums|header
AstaHost|www.astahost.com/index.php
Trap17|www.trap17.com
AntiLost|www.antilost.com
Online References|header
Dictionary|www.dictionary.com
Thesaurus|www.thesaurus.com
Maps|www.mapquest.com
Phone|www.sbc.com
Alta Vista|www.altavista.com
Excite|www.excite.com
Google|www.google.com
Lycos|www.lycos.com
Yahoo|www.yahoo.com
Public Forums|header
AstaHost|www.astahost.com/index.php
Trap17|www.trap17.com
AntiLost|www.antilost.com
Online References|header
Dictionary|www.dictionary.com
Thesaurus|www.thesaurus.com
Maps|www.mapquest.com
Phone|www.sbc.com
Note here that we use the pipe "|" charater to seperate the link name from the link url. We will write our script to read each line individually so each line effectively acts as the records seperator.
Here is the code we will use in menu.php
CODE
<?php
function make_menu($name, $url) {
if ($url == 'header') {
$link = "<span style=\"font-color: lime; font-weight: bold;\">" . $name . "</span><br>";
}
else {
$link = "<a href=\"http://" . $url . "\" style=\"font-size: 8pt;\"> ♦ " . $name . "</a><br>";
}
return $link;
}
$handle = @fopen("databases/menu.db", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle);
$menu_array = explode("|", $buffer);
echo make_menu(trim($menu_array[0]), trim($menu_array[1]));
}
fclose($handle);
}
?>
function make_menu($name, $url) {
if ($url == 'header') {
$link = "<span style=\"font-color: lime; font-weight: bold;\">" . $name . "</span><br>";
}
else {
$link = "<a href=\"http://" . $url . "\" style=\"font-size: 8pt;\"> ♦ " . $name . "</a><br>";
}
return $link;
}
$handle = @fopen("databases/menu.db", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle);
$menu_array = explode("|", $buffer);
echo make_menu(trim($menu_array[0]), trim($menu_array[1]));
}
fclose($handle);
}
?>
The first part of menu.php is nearly the same as the one used in CMS101 - Content Management System Design.
The second part of the script replaces the old individual make_menu() funtion calls with code that will read data from the database and automatically pass the information on to the make_menu() function. This code first opens the file for reading and then begins to read from the first line. Once the first line is read and the data is processed, the code goes to the next line. Prior to reading any line, the script first checks to see if there is data on that line. This is done with the feof() function to see if the end of the file has been reached. As long as the end of the file has not been reached, the script will read each line and pass that lines data to the make_menu() function. The explode() function reads the line and breaks the data into each field based on the location of the pipe character. The trim() function ensures that only the data is passed to the make_menu() function be removing all whitespace before and after the data.
The benefit of using this type of database system to store the data for your menu is that the database is very easy to read. When editing the menu, all you need to do is insert or delete lines of data where you want the menu item to be located. For those that are less interested in scripting and programing, there is an added bonus of not having to worry about damaging the script each time you edit the database since the two are seperated from one another.
It should now be getting clearer how using a template or CMS system on your website can make the day to day administration of your website much easier. Please understand that this tutorial is not a template layout and design how-to. It is a tutorial on how to create a template system. For information about template layout, design and/or style, please look at forum categories related to website design. This is a PHP scripting how to. I'm not very good at the overall look and fell of website design. Please don't ask me about that type of stuff.
I hope that this information will be helpfull.
vujsa


