Jump to content
xisto Community
Sign in to follow this  
vujsa

CMS102 - Content Management System Design Basic CMS With PHP Flat File Databases

Recommended Posts

This topic is a follow up to CMS101 - Content Management System Design and PHP Tutorial: Menu Or Sidebar Script For CMS101

[hr=teal] [/hr]

Overview:


[/hr]

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:


[/hr]

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:


[/hr]

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 http://forums.xisto.com/no_longer_exists/. 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. http://forums.xisto.com/no_longer_exists/?page=links should display a page with a list of links on it. However, http://forums.xisto.com/no_longer_exists/?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 http://forums.xisto.com/no_longer_exists/?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.

<?php$page= $_GET['page'];$content_file = 'content/pages/'. $page . '.php';// Now we check to see if that file existsif (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:


$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 linenums:0'><?php$page= $_GET['page'];$content_file = 'content/pages/'. $page . '.php';// Now we check to see if that file existsif (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:

http://forums.xisto.com/no_longer_exists/?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:

<?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 variableif (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 variableif (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.

 


[/hr]

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

Search Engines|headerAlta Vista|altavista.comExcite Forums|headerAstaHost|http://forums.xisto.com/index.phpTrap17|www.trap17.comAntiLost|www.antilost.comOnline References|headerDictionary|dictionary.comThesaurus
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

<?phpfunction 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. :lol:

 

vujsa

Share this post


Link to post
Share on other sites

Danggit, Vujsa beat me to it: http://forums.xisto.com/no_longer_exists/

 

The Source Code for the scripts are included (literally) on the different pages on the Demo, including the Contact / Email script. The only page not there yet is the Message Script. Maybe tonight I will upload that.

 

This one uses a little bit different approach. It is a little simpler in spots (no header change-up), but does a little bit of query-string checking to confirm that the contents of the page are actually available (file_exists())and an allowed page content before serving it up. The 'allowed page content' is done by checking against a flat-file containing an array of acceptable query-string contents. I had another part of the script working to check against the contents of the third column of the menu_data.txt file, but it fails on the Xisto server and I haven't sourced the problem, yet, so this array check is a temporary measure.

 

Also, there is a Conact, or email script which writes to a text file as a repository for messages received from the site in case for some reason the Mail() doesn't work for the Hosting service. (ie: Xisto.net disables the mail() on the Free Hosting Accounts) I have written a script to extract and print them also, maybe a Tutorial about that will be next?

 

Also, I have used a 'fixed' header and sidebar approach to the template design which some of you might find interesting. There is a conditional statement there to utilize a different css file for the page structure of IE, due to the Browser differences, this was the way to make it 'cross-browser friendly'. Since I don't have an IE6 Browser, I will only state that the responses I have received are positive. I haven't actually seen the page in an IE6 Browser yet, so I don't know if it explodes or not. If the Alt= for the menus are removed, the page validates to the w3c standards set in the DTD. (html 4 strict).

 

Can someone tell me if IE6 still requires the Alt= for tooltips? Or can they be removed and have the title= displayed instead?

 

Anyway, thanks Vujsa for your Original Topic. I found it quite valuable as a guide to learning the php required for this scripting and it has opened up an entirely new perspective on the Web Sites I intend to build in the next short while. I hope someone else might be encouraged by this sample I have added to the Topic. And as Vujsa stated up in his Posting, the greatest advantage of viewing these codes and Templates is not in 'sniping' the code to install the files as a website, although it is possible to do that, the greatest value to be received is as an enticement to proceed on the path to learn the language and develop an understanding of the uses for the tools which php provide. If you want to use the code, PM or email me and I'll zip a set to you, but you wil receive much more benefit from writing your own Templating System. And it will be easier to modify or maintain because you are familiar with the parts, what they do and how they operate. Having said that, if you want / need a copy of the code, let me know and I'll pass you the zip file.

 

Thanks for showing some interest in my scripts.

Share this post


Link to post
Share on other sites

Nice. I've never played with flat file databases, or modifying files from php in general. I usually use mysql or postgres to store most of my stuff. For menu purposes a flat file seems to be a valuable asset to the system.I was thinking about building a image gallery script. I currently have an image upload script, but I downloaded an image gallery. I like that it creates its own thumbnails and even 50% and 75% versions of the image. I would like to create an image gallery that creates it's own thumbnails.Here's a quick layout of what I want:Database Table -IMGID -IMGURL -THUMBURL -NOTESPHP script - to upload -upload image -create thumbnail of image -write image url, notes, thumbnail url to databasePHP display script -Pull imgs and thumbnails and notes -display 5 per page or so <-any specific way?Any advice on this? I know it doesn't have anything to do with CMS but it could be built into a CMS. Main reason would be displaying an item database of current and available items for sell.

Share this post


Link to post
Share on other sites

Danggit, Vujsa beat me to it: http://forums.xisto.com/no_longer_exists/
........

Anyway, thanks Vujsa for your Original Topic. I found it quite valuable as a guide to learning the php required for this scripting and it has opened up an entirely new perspective on the Web Sites I intend to build in the next short while. I hope someone else might be encouraged by this sample I have added to the Topic. And as Vujsa stated up in his Posting, the greatest advantage of viewing these codes and Templates is not in 'sniping' the code to install the files as a website, although it is possible to do that, the greatest value to be received is as an enticement to proceed on the path to learn the language and develop an understanding of the uses for the tools which php provide. If you want to use the code, PM or email me and I'll zip a set to you, but you wil receive much more benefit from writing your own Templating System. And it will be easier to modify or maintain because you are familiar with the parts, what they do and how they operate. Having said that, if you want / need a copy of the code, let me know and I'll pass you the zip file.

Thanks for showing some interest in my scripts.

Your tutorial site is comming along very well. I like that your content is the how to and source code for the page being displayed. Sorry that I beat you to the punch. I've been wanting to do a proper follow-up on the original topic for a while now but didn't have the time before. I do think that your tutorials do a very good job of taking my articles to the next level. You tend to give the practicle application side of the story. It is good to see that my information has inspired others to learn PHP or at tackle a larger project for the first time.

While I'm sure I'll get a little side tracked on the topic from time to time as I answer questions, I think I am still on the path that I want to be on for this series of topics. The real focus of this series of articles is to show just how little PHP can be used to get major benefits. I think that so far I used about 40 lines of actual PHP code and about the same for HTML. The hardest part for me is trying to remain general enough in my examples to allow my readers to see alternate uses for the scripts. I learned all of my PHP knowledge from adapting and/or combining various scripts to get what I want.


[/hr]

Nice. I've never played with flat file databases, or modifying files from php in general. I usually use mysql or postgres to store most of my stuff. For menu purposes a flat file seems to be a valuable asset to the system.
I was thinking about building a image gallery script. I currently have an image upload script, but I downloaded an image gallery. I like that it creates its own thumbnails and even 50% and 75% versions of the image. I would like to create an image gallery that creates it's own thumbnails.

.......

Any advice on this? I know it doesn't have anything to do with CMS but it could be built into a CMS. Main reason would be displaying an item database of current and available items for sell.

Well, I chose to use the flat file database because I still think it is a very valuable system to learn from. Before mySQL was availible and all of the other database systems were expensive and/or difficult to use, we all used the flat file database. The down side of it was if you got a lot of data in your database combined with a lot of traffic on your site, the server got bogged down frequently. Imagine thousands of people all trying to search your database with thousands of records at the same time. Needless to say, MySQL runs much faster
than the old system but if you don't have a lot of data to store, then you might want to consider using the flat file system. A good example of this would be a counter. If you don't already use a MySQL database, it would be silly to set one up just for a one entry, one field counter. Even if you wanted to have a counter for every page, the database would remain very very small.

I highly recommend that everyone interested in learning PHP take interest in using the file system functions for PHP. Being able to manipulate your files will offer you many more possibilities in the end.

As for your image gallery project, I think I can offer some help. Have you read my article about image gallery creation? Creating Your Own Image Gallery With Php. It isn't a source availible tutorial like this one and the type of gallery is different but many of the same functions are used that you will need. It was written for people that have large image directories to be able to learn how to display their images to others so it is based on a different viewpoint than your question.

It looks at the directory and begins by checking to see if the images have thumbnails already. If the thumbnail exists, the thumbnail is displayed. If the thumbnail does not exist, then one is created and then it is displayed. This is better in most cases than creating a thumbnail every time an index of images is shown since that would overload the server easily. If you want to have 50% and or 75% sized images, it may be better to generate them on-the-fly or only upon request since saving the extra images can eat up a lot of storage space but the images wouldn't be requested narly as ofter as the thumbnails would be.

The best idea for you I think is to generate a thumbnail right after the image is uploaded. You should still check to be sure that you have a thumbnail for each image when you display your index and create the thumbnail if missing but that should be a failsafe system. In order to display a certain number of images on a page and provide a link to the next page with the next images, you need to keep track of which image was shown last on this page and provide that information to the next page either through the url or a form. You will display your images using a loop that you will need to stop when the correct number of images are shown on the page. You can card code the number or allow your user to specify the number to show. Also, if you want to show 15 images, you probably want to have 3 rows of 5 images. If this is the case, then you'll need a secondary loop that will control the number of rows used.
For example if $x controlls the total images and $y controls the number of images in a row:
$x = 1;$y = 1;if ($x <= 15){	if ($y<=5) {		 // INSERT IMAGE HERE		 $y++;	 }	 else {		 $y = 1;		 // INSERT NEW ROW HERE		 // INSERT IMAGE HERE		 $y++;	}	$x++;}
That will only show 15 images. Be sure to add a condition to check and see if there is an image to show. You don't want an error message if you only have 14 images in your directory!

Well that's all for now. I'll add more to the topic later but now it's time for bed.

Hope this helps. :lol:

vujsa

Share this post


Link to post
Share on other sites

Well, I chose to use the flat file database because I still think it is a very valuable system to learn from. Before mySQL was availible and all of the other database systems were expensive and/or difficult to use, we all used the flat file database.

Yes, I remember them, too. And using cassette recorders for program and data storage. Everything came out sequentially off the tape. It could take a while to find the right spot on the tape...
And I absolutely agree with the comments about 'wasting'a DB to store a couple records for the menu info and other small pieces of info like page counters and all. Unless maybe using it as a training tool??

So, Vujsa, how does the x counter increment in that Photo Gallery Script?

Share this post


Link to post
Share on other sites

Why does it show this?

 

Notice: Undefined index: page in C:\Inetpub\wwwroot\index.php on line 2

Notice: Undefined index: header in C:\Inetpub\wwwroot\index.php on line 3

 


Well, I think it is because you didn't specify which page and header you wanted in the url.

 

Here is line 1, 2, and 3:

<?php$page= $_GET['page'];$header= $_GET['header'];

You would use a URL like this: http://forums.xisto.com/no_longer_exists/ to specify the page name and header name.

 

That is what lines 2 and 3 are looking for in the code.

$_GET['page'];
means get the variable form the url that is assigned to page:

http://forums.xisto.com/no_longer_exists/?page=main&header=header1

$_GET['header'];
means get the variable form the url that is assigned to header:

http://forums.xisto.com/no_longer_exists/?page=main;header=header1

 

As a result here is what your code actually would see if you used that URL:

<?php$page= "main";$header= "header1";

To prevent this from being a problem in the future, you should set default page and header files.

 

<?phpif($_GET['page']){	 $page= $_GET['page'];}else{	 $page= "default";}if($_GET['header']){	 $header= $_GET['header'];}else{	 $header= "header_default";}$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 variableif (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 variableif (file_exists($header_file)) {	 $main_header_file = $header_file;}else {	 $main_header_file = 'content/headers/header_default.php';}?>

That is rather crude but will ge the job done I think.

 

There are a number of security prcautions that you should probably look into if you will be using this type of input to control you pages.

 

More information can be found in the CMS103 tutorial.

 

Hope This Helps. :D

 

vujsa

Share this post


Link to post
Share on other sites

Here is a link to the search results for tutorials in the PHP section that I contributed to:
Advanced Search: user=vujsa; Category=Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > PHP

Hope you find these usefull.

If there are specific topics that you would like me to discuss, there is a tutorial request section that you can post in.
Other questions can be addressed in the PHP forum.

vujsa

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.