Jump to content
xisto Community
WC2MyWorld

Displaying Html Stored In Mysql Using Php Something missing (perhaps?) in a basic tutorial...

Recommended Posts

Hi Everyone!

 

Firstly, while I have experience with HTML/CSS and have dabbled a little in PHP/MySQL, I'm certainly not an expert! LOl!

 

I have always written every page by hand and only used templates in CSS.

 

I'm now attempting to use PHP/MySQL to store/retrieve HTML templates. I have been trying what seems a nice clear & simple method I found at one of the many respected online tutorial websites. As it is a website completely outside of this awesome community and I believe [rightly or wrongly - please correct if necessary] I cannot post the link here therefore I will have to try to explain the tutorial and then my issue.

 

The tutorial explains the MySQL query to set up the table and then displays the following MySQL code:

 

CREATE TABLE template (template_id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, Head_Open VARCHAR(60), Head_Close VARCHAR(60), Page_End VARCHAR(60), Date VARCHAR(60));  CREATE TABLE content (content_id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, title VARCHAR(60), body MEDIUMBLOB); 

Next the author explains and displays the MySQL query to add some data to the tables:

 

INSERT INTO content (title, body) VALUES ( "MyPage", "This is my sample page, where I will use PHP and MySQL to template my website. <p> Here is a list of things I like <ul><li>PHP Code</li><li>MySQL</li><li><a href=http://php.about.com>PHP @ About.com</a></li></ul><p> That is the end of my content.") ;  INSERT INTO template (Head_Open, Head_Close, Page_End, Date) VALUES ( "<html><head><title>", "</title><body>", "</body></html>", "$b = time (); print date('m/d/y',$ . '<br>';"); 

Next the tutorial explains and displays the following PHP code:

 

<?php  // Connects to your Database  mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());  mysql_select_db("Database_Name") or die(mysql_error()); //This retrieves the template and puts into an array. No WHERE clause is used, because we only have one template in our database. $coding = mysql_query("SELECT * FROM template") or die(mysql_error()); $template = mysql_fetch_array( $coding );//This retrieves the content and puts into an array. Notice we are calling ID 1, this would change if we wanted to call a page stored on a different row $text = mysql_query("SELECT * FROM content WHERE content_id =1") or die(mysql_error());$content = mysql_fetch_array( $text );//Actually puts the code and content on the page Print $template['Head_Open'];Print $content['title'];Print $template['Head_Close'];//When pulling PHP code we need to use EVAL Eval ($template['Date']);Print $content['body'];Print $template['Page_End']; ?> 

Obviously the script itself is rather well commented and simple to understand each line of code. The author does not, however, show or explain how to actually implement the PHP to display the result as a templated document/webpage. Perhaps the article was written for a more advanced user than myself ;p

 

Can someone show me how to implement the PHP code please?

 

I have entered the above PHP code into a file called 'template_001.php'.

No matter how I try to call on this file the result is either a blank white page or the prompt to open/save/edit the php file.

I'm using XAMPP 1.7.3 & Firefox. No changes to the XAMPP or Firefox installation are necessary as ONLY this script is not working. The problem is merely something I'm not doing right in the implementation of the script itself.

 

I have tried using it as an 'include' & as a 'require', however, I still have no HTML displayed.

 

The economics of this scripts usefulness is less important than understanding how to use it successfully.

 

I would be most grateful if anyone could explain how to make this work.

 

Cheers!

 

Thankyou also to 'TrueFusion' for his reply as it prompted me to edit my question to clarify the outcome I need. Cheers!

Edited by WC2MyWorld (see edit history)

Share this post


Link to post
Share on other sites

The code you posted shows how to retrieve the template code from the database in the following lines:

mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); mysql_select_db("Database_Name") or die(mysql_error()); $coding = mysql_query("SELECT * FROM template") or die(mysql_error()); $template = mysql_fetch_array( $coding );$text = mysql_query("SELECT * FROM content WHERE content_id =1") or die(mysql_error()); $content = mysql_fetch_array( $text );

The code you posted shows how to display the template code in the following lines:

print $template['Head_Open']; print $content['title']; print $template['Head_Close'];print $content['body']; print $template['Page_End'];
As you have already figured out, include() and require() will not work, because these two functions do not concern any database but files in the file system.

 

There is really not much use in storing a template inside a database, though. To me, that just increases the complexity of the template system. If you store the template code in regular text files, then include() and require() will work, and the template system would be easier to manage.

Share this post


Link to post
Share on other sites

I agree, I don't see any need to store templates in database with HTML code, it's in most cases useless. I suggest to store your templates in a separate folder with the name of the template in a directory templates, that way you have an easy control over it.Put your HTML files there and you can use some-kind of a template engine to replace the values or include the values you need, for example:HTML code {content} HTML codThis is your template, from a database you get your content and put it/replace it with {content}, so you have the real thing and it's much easier to edit your HTML template files in a file rather than in a Database.

Share this post


Link to post
Share on other sites

I read your posted content and can safely say that the code is not practical. Storing templates on database are way faster if your database server is powerful enough (specially on cases of shared hosting) and the database data was properly indexed. The problem arises when you install a large portion of your template on your database, the proper way to do it is to store snippets or segments instead of the whole page.On the problem of using includes and other PHP commands, you can execute them by manually invoking the proper calls. I would agree on everyone who replied that this adds more complexity to the template system. The fastest speed gain you can get with calling the templates from the database in comparison to calling them via folders is just around 5%-20%. The loss you can get if you handle it improperly is a slow down of 50%-300%. You need to plan way ahead and must understand how database and the server interacts with each other, you also need to plan how much power your database have (cpu, memory and number of users).My point of not including the PHP call that you need to execute is to discourage you to this risky approach.

Share this post


Link to post
Share on other sites

Also, to add more, if you're not using a template from files, but from a database, in a database error, you won't get any template and you're error will be black font on a white background :DEven though it's not so relevant, but with a PHP template system from files, you can show that error by using your template. :POf course, it's rare cases, that mysql can be down.

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

×
×
  • 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.