Flag This Hub

Generate Static HTML Pages From A Database

By


Static HTML vs. Dynamic Pages

I have a rule, when it comes to webpages. Keep it simple. No scripts, no flashy templates. It's no big secret that the most successful websites are those which are the simple and easy to use. See Google. But that doesn't mean that you can't leverage technology behind the scenes to provide users with a better experience. The specific problem I'm addressing here is whether to use simple and static HTML pages, or generate pages dynamically from a database, using PHP/mysql.

Static HTML pages have obvious advantages - Fast loading, easy to write, less load on the server, lower bandwidth costs, search engines prefer static pages over dynamic ones ( generally speaking ).

Dynamic pages have their own set of advantages - Automatic refreshing of content ( news, sidebar updates, etc. ), capability to interact with the user ( web query forms, data output, ecommerce applications, etc. ).

But I want to have the best of both worlds. I want to have a static HTML page, which is served out of a database, and will refresh itself automatically at pre-specified intervals. How is this done?

You don't need to be a programmer to implement this on your tiny personal website. Anyone can do this. You just need to have a webhost who allows you to run PHP scripts, a mysql database and do some trial and error.

I'm assuming that you have need of a database driven application on your website. It's overloading your server or loading real slow, and the search engines don't like it. Which is why you want to switch to static, but retain the database output. If you don't have this need, well, head over to entertainment to check out how Paris Hilton is doing ( She's doing fine - In an LA Prison ). Update : Psychiatrist rushes to see Paris. Paris Hilton is dynamic and the search engines love her. PHP code vs. Paris Hilton - No contest this...

Take one of your PHP page as a test subject. All you need to do is add a bit of code to the top and bottom of the page, as shown below.

PHP Code

PHP code to generate static HTML pages from a database
PHP code to generate static HTML pages from a database

Explanation

What we're doing here is basically generating a cache of the parsed output of a PHP page. Change 'your-folder/your-file.html' to point to the location and name of your static html file. The folder and file should have write permissions ( 666 ).

ob_start() starts the caching process.

Just below this is where the entire contents of your original PHP page should be.

End of that, add the code snippet as shown in the image above.

And that's that. You should now have a static HTML file which is the same as the parsed output of the original PHP page. Notes : If you want to save even more bandwidth, just set up a cron job to call the PHP page once in 24 hours, and you can skip the code for the cachetime. So, in that case your code would be something like this :

PHP Code 2

Static HTML page generation from database using cron job
Static HTML page generation from database using cron job

Summary

So what we have here are static HTML pages, which can interact with a visitor, take some input parameters and spit out data from a database. They will referesh automatically to reflect any changes in the database. Your visitors get pages which load real fast, your hosting bill goes down and server is now able to handle more queries without choking. Best of all, search engines love the pages, because these are static HTML pages whose contents keep changing. What could be better than this?

Comments

johnr54 4 years ago

For those looking to do this offline, Webmerge is a pretty good solution that runs on multiple platforms.

George 3 years ago

Or a better way is to have an .htacces to run html files as php

Andrew 2 years ago

Does this come under the category of black hat techniques?

I have a list of towns and cities that I would like to spit out as static html pages but feel it may be spammy?

Andrew

Peter 2 years ago

Good site, thanks for the info

Selva 22 months ago

Is it possible to make this work with wordpress?

peeling 22 months ago

Selva - It works on any PHP page, but since wordpress already offers dynamic .html pages, I don't see why you'd need to do this on wordpress.

free language translator 21 months ago

Well, the article doesn't explain how to generate static pages starting from a database (i mean a database like MySQL or MSSQL), but instead it explain how to use a file-database system. In more cases a file-database solution can be the right choose against using 'standard' databases, due to its high speed (there is NO database that crunch the server processor). Of course, the updating/generation mechanism of such a website should be very well planned from the very beginning in order to avoid massive manual updates.

@Andrew: this has nothing to do with BH

Mark 21 months ago

Noticed your comments and this is something I'm trying to work out myself. I use CofferCup's HTML editor and have been building my own sites for about a year, so I've got a lot to learn.

I like the idea of static pages but it seems so much more sensible to publish these from a database before they are uploaded to my web server. Managing the content in a database appears so much easier but how can you do this easily?

Document Management Software 15 months ago

Thanks for your advice. I eventually purchased a web merge for the project I was working on and it built me over 1100 pages in about 5 minutes - really saved my bacon.

Thanks again

WebDiva 13 months ago

Thank you! Exactly what I needed -- perfect for generating static files from WordPress 3.1 wp_nav_menu() so I can have the same menus on external pages! I kiss your feets!!

Tech 13 months ago

Hey peeling man you made my day. was searching and got the solution for php to static html pages. Your simple code worked gracefully.

Thanks for sharing the knowledge.

Best Regards

Riad 7 months ago

Many Thanks for this post. saves a day!

tamouse 6 months ago

This is pretty cool, but what would be cooler would be to automate this so you don't have to wrap each existing php file that may generate output. Also -- this should only be done for files that *do* generate output, obviously.

Douglas 4 months ago

I'm trying to do this for a few pages that originally html pages that have php included in them so had to have the php extension on them. So, the code encountered an error upon trying to read the doc type tag above the head section. Any way to do what I want?

Cameron Reid 3 months ago

Nice post. Here’s a tool that lets you build your online database in minutes, without coding http://www.caspio.com/

sreenuvasulureddy 2 weeks ago

allok

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working