As I understand your question, you are wanting to save the various components of your template in a database instead of the file system. Is this correct?
Well, you don't use includes for that operation. Instead, you will use various mySQL functions to retrieve the data.
In
CMS102 - Content Management System Design, Basic CMS With PHP & Flat File Databases I showed you how to use a dynamic URL to determine which content to display on your website.
For example:
www.mycms.com/index.php?page=news&header=header1
Would show the standard webpage for your site with the
news.php file as the content and the
header1.php file as the page header.
What I think you are wanting is to save the data in
news.php and
header1.php in a database table instead of in a file.
So instead of creating a file and adding data to it to save in your file system, you need to create a new record in your database.
If
news.php had the following data in it:
CODE
<b>D-Day!</b><br>
<i>By vujsa</i><br>
June 7, 1944<br>
<br>
Yesterday the Allied forces invaded Normandy, France in a effort to gain a foothold in the war with Germany.....<br>
<br>
<br>
<b>Japan Attacks Pearl Harbor!</b><br>
<i>By vujsa</i><br>
December 8, 1941<br>
<br>
Sunday, December 7, 1941; The Japanese navy launched an attack on Perl Harbor near Honolulu, Hawaii. The attack lasted....<br>
<br>
<br>
etc, etc, etc,...
Then instead of savinf that information as a file, you would save it into your database table. You'll need some structure to your database table to get it to work correctly. At the very least, you need 2 fields (columns).
"name" and "body".
So first we need a database table to work with. If you don'tknow how to create a table or a database for this operation, please search the forums before continuing.
We'll name our content table "mycms_content". We'll use the "mycms_" prefix for all of our tables related to the CMS so that we don't get confused later!
In "mycms_content", we need to create a field named "name" as the type "tinytext". Then we create a second filed for the data named "body" as the type "text". Although we really don't need it, we will also ad a field for the record id. This isn't needed as long as the table remains simple but if additional fields are added later, then we'll need this. Might as well plan ahead now.
The third field is "id" and is of the type "tinyint"; it should auto_increment , be the record key and should be unique!
Here is the SQL command for creating the required table:
SQL
CREATE TABLE `mycms_content` (
`id` TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` TINYTEXT NOT NULL ,
`body` TEXT NOT NULL
) TYPE = MYISAM ;
Then you just insert the data and name of your content into the table with the following SQL command:
SQL
INSERT INTO `mycms_content` ( `id` , `name` , `body` )
VALUES (
NULL , 'news', '<b>D-Day!</b><br> <i>By vujsa</i><br> June 7, 1944<br> <br> Yesterday the Allied forces invaded Normandy, France in a effort to gain a foothold in the war with Germany.....<br> <br> <br> <b>Japan Attacks Pearl Harbor!</b><br> <i>By vujsa</i><br> December 8, 1941<br> <br> Sunday, December 7, 1941; The Japanese navy launched an attack on Perl Harbor near Honolulu, Hawaii. The attack lasted....<br> <br> <br> etc, etc, etc,...'
);
We now do the same for our header1 data!
SQL
INSERT INTO `mycms_content` ( `id` , `name` , `body` )
VALUES (
NULL , 'header1', '<a href="http://www.astahost.com"><img src="http://www.astahost.com/style_images/astalogo4ly.gif"></a>'
);
Basically that is just a banner ad for AstaHost!

Congratulations, you now have your content in your database. But how do you get it out now?
Going back to our sample URL: www.mycms.com/index.php?page=news&header=header1
We start our PHP script in the usual way but then it will change quickly!
CODE
<?php
$page= $_GET['page'];
$header= $_GET['header'];
?>
Now all the script is doing is finding out what page and header were requested. We have to do a database query to find the required content.
Here is the SQL command to use to find our "news" content:
SQL
SELECT `body`
FROM `mycms_content` WHERE `name` = 'news'
LIMIT 0, 30
In PHP, that would look something like this:
CODE
$sql = 'SELECT `body` FROM `mycms_content` WHERE `name` = \ 'news\' LIMIT 0, 30 ';
So for us to fully retrieve our content and use it, we do the following:
Some of this is basic SQL scripting that I won't go into here. Learn more about it by searching the forums.CODE
<?php
$page= $_GET['page'];
$header= $_GET['header'];
$connection = @mysql_connect("localhost", "username", "Pa5Sw0rD") or die(mysql_error());
$db = @mysql_select_db("My_DataBase", $connection) or die(mysql_error());
$sql = "SELECT `name` FROM `mycms_content` WHERE `name` = \ '". $page ."\' LIMIT 0, 30 ';
$result = @mysql_query($sql, $connection);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
$main_content = $row[0];
$sql = "SELECT `name` FROM `mycms_content` WHERE `name` = \ '". $header ."\' LIMIT 0, 30 ';
$result = @mysql_query($sql, $connection);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
$main_header = $row[0];
?>
Okay, that is the most direct and ugly way to get your data from the database! All we did was search the database to get the body of the file who's name matched what the value was in the dynamic url. In this case we searched for "news" and "header1"! That definitely needs some optimization later but now let's just get to the content insertion section.
In CMS102, I said that where ever you wanted your main content, you would use this:
<?php include($main_content_file); ?>! That has now changed since you are no longer going to include a file but instead you will be echoing a string of content. Now where ever you want your main content, use this command:
<?php echo $main_content; ?> The same is true for the positioning of your header data but the variable would be $main_header!
That is the rough explaination of the system. You would still have to design a database interface for inserting, editing, and removing content from your website but that is a whole other discussion for later I guess.
I reccommend writing a function to handle your database query instead of writing a new query for each content section of your website. The key is to have the PHP handle as much of the work as possible so using functions and loops will save you a lot of work in the long run.
Let me know if there is more information that you need about this.
vujsa
Reply