Jump to content
xisto Community
Sign in to follow this  
Neyoo

Vujsa's CMS101 And CMS102 Tutorials Where is the link between cms101 cms102 tutorials

Recommended Posts

I have read Vujsa's tutorials on cms101 and cms102. As a beginner to cms, php and mysql this tutorial has been very helpful. Thanx for Vujsa, really appreciate what you've done for guys like.But I have had a problem with the missing links. How do I integrate this tutorial with tables I create in php/mysql database. I understand the includes, the problem is with the files I would have create in mysql table. How do I link to those files using includes and is there a management page for the files I create using mysql. Thank you as I await your reply.

Share this post


Link to post
Share on other sites

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:

http://forums.xisto.com/no_longer_exists/

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:

<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:

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:

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!

INSERT INTO `mycms_content` ( `id` , `name` , `body` )VALUES (
NULL , 'header1', '<a href="http://forums.xisto.com/
src="/style_images/astalogo4ly.gif;");
Basically that is just a banner ad for Xisto! :P

 

Congratulations, you now have your content in your database. But how do you get it out now?

 

 

Going back to our sample URL: http://forums.xisto.com/no_longer_exists/

 

We start our PHP script in the usual way but then it will change quickly!

<?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:

SELECT `body`FROM `mycms_content` WHERE `name` = 'news'
LIMIT 0, 30

In PHP, that would look something like this:

$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.

<?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

Share this post


Link to post
Share on other sites

Thanks Vujsa,Your reply was very instructive. Now I realise I've learnt something new, talking about cms101 and cms102. However your recent tutorial just won't work. What I did was to copy the php and sql script and using my xampp, see if everything would work but I kept on getting errors. I have looked carefully at the script and tried to changed the codes but still no luck. Would you pls shed more light on the tutorial as I'm only a beginner. But I can create database and mysql tables. It's the manipulation of the scripting to produce dynamic sites with addresses such as (http://forums.xisto.com/no_longer_exists/) or (http://forums.xisto.com/no_longer_exists/) that I'm looking to really learn.Learning these basics would surely help me on. Thank you as always for what you are doing.Neyoo

Share this post


Link to post
Share on other sites

Thanks Vujsa,

 

Your reply was very instructive. Now I realise I've learnt something new, talking about cms101 and cms102. However your recent tutorial just won't work. What I did was to copy the php and sql script and using my xampp, see if everything would work but I kept on getting errors. I have looked carefully at the script and tried to changed the codes but still no luck. Would you pls shed more light on the tutorial as I'm only a beginner. But I can create database and mysql tables. It's the manipulation of the scripting to produce dynamic sites with addresses such as (http://forums.xisto.com/no_longer_exists/) or (http://forums.xisto.com/no_longer_exists/) that I'm looking to really learn.

 

Learning these basics would surely help me on. Thank you as always for what you are doing.

 

Neyoo

 

Okay, I guess I may have under estimated your level of programming when I replied to you. Having a hard time with some of your questions. Seems like you have left some key factor out of your posts. I'm not usre what we are missing from the first post but in the second post, I know that I'll need to know the error messages you are getting in order to figure out the problem.

 

Most of my tutorials are very general to leave users with a roadmap to building their own script however they fell most comfortable. As a result, I don't discuss error checking and security measures in any tutorials since everyone has their own method. I also don't go into much detail on script extras since they may not be used by the end reader. If you want to add something on to a tutorial, I think I leave it basic enough to adapt later. If I get too specific, then the script is much more difficult to adapt to the user's needs later. Basically, I discuss techniques and methods rather than actual scripts.

 

Having said that, some time when I'm in a hurry, I don't actually run the scripts you see in the posts I write. I'll just start typing in the post box and use logic and my knowleged of php to output code for the topic. In the case of my previous post on this subject, I never tested the script and tested very little of the chunks of code used. I ran a few database queries to be sure I had the SQL correct but none of the PHP has been tested. :P

 

So I don't have a working copy of a script for you to test on my server. So I'll need you to provide any error messages you have to me so I can figure out what went wrong.

 

I'm sorry it has taken so long to reply to you. Kind of a busy time of the year for me personally and professionally.

 

As for you date problem you requested help on, I'm not sure what the problem is without seeing some code but I'll give it a try.

 

Usually, when I save a date to the databse, I prefer to use UTC which is the number of seconds since the Epoch (01/01/1970). It usually is a rather long number and is based on GMT so it will be the same for everyone everywhere and can be displayed based on the local timezone for local output to the user.

 

This value can be obtained for the current time with time();

Now when you want to display a date, you have to use the date() function but have to specify the timesatmpt used or the current date and time will be used.

 

If the current time is 12/18/2006 09:30PM and you use the following:

echo date('Y-m-d');
2006-12-18 will be displayed!

 

But if you use a timestamp with the date like so:

echo date ('Y-m-d', 1166365800);
2006-12-17 will be displayed.

 

So if you don't specify a timestamp, the current time is used by default.

 

If this doesn't answer your question, let me know.

 

vujsa

Share this post


Link to post
Share on other sites

Hello Vujsa,Thank you very much for your detailed reply. I picked up a tutorial somewhere else just before I stumbled upon yours and that has helped me resolve the dynamic urls I wanted to be able to create. Infact I now use a combination of what your last tutorial discussed and your static php page includes. I have almost finished a complete redesign of my alumni site.But the date problem is still unresolved. This is the code I used - $postdate = date('d-M-Y'); I created the postdate table field in my database and assigned date value to it. But all I get is the date keeps changing to my system date not matter and NEVER when the post was actually made. It's the last piece of problem preventing me from posting my site. I look forward to your help.Thanks,Neyoo

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.