Generate Static HTML Pages From A Database

85
rate this page

By peeling


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?

  —   Rate it:  up  down  [flag this hub]

Comments

RSS for comments on this Hub Small RSS Icon

johnr54 profile image

johnr54  says:
3 months ago

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

Submit a Comment

Members and Guests

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


optional



working