Databased Websites from Scratch

Tweet

Much has been written about building and maintaining data-driven Websites. These sites are typically seen to involve three tiers. The term ‘three tier’ refers to the metaphorical layers within a data driven Website:

Content, Program Logic, and Presentation

‘Content’ refers to all the text in the site – not the navigational links or images, but the raw textual information, such as news articles or product descriptions.

‘Program logic’ refers to the code that handles user requests for pages and displays them according to certain rules, such as: if the request comes from a Linux machine — show this page.

‘Presentation’ refers to the code that handles the formatting of your content and displays it in an appropriate format, like HTML or XHTML.

My site, for example, is stored on a MySQL database. Page requests are handled by the PHP programming language and the presentation is handled by templates that contain HTML.

Benefits of a Data-Driven Solution
  • Separating the content from the site’s presentation allows you to display that content in different ways (such as through wireless devices), or to display browser-specific templates (see below).
  • Templates allow you to change the appearance of an entire site in minutes. If you decide that a link needs to be added, or you make alterations to your copyright text, you can update one file (rather than hundreds) and affect the whole site instantly.
  • Maintenance and editing can be performed by anyone who can fill out an online form — you don’t need editorial staff to know HTML or, in fact, any code at all . If you need to update product descriptions, for example, you can use a simple form that can be designed specifically for that purpose.

You may notice that I haven’t made any mention of queries or ecommerce database functions here — they’re a little beyond the scope of this article.

Who Needs a Database?

If your site’s content will be changed or added to more than once or twice a month, then you should consider using a database to store the content, and building an online administration program to manage it. The solution requires a little more work at the outset, but it will save you time and money in the long run.

If you intend to maintain a very small and fairly static site (such as an online business card) then stick to the traditional static approach — this should be adequate for your needs.

If you want to build a data driven site, or just find a little more technical information on the topics we’ve touched upon, try these resources:

www.mysql.com
www.php.net
www.phpbuilder.com
www.devshed.com
Webmasterbase

Ok, so now you’ve decided that you need a dynamic, flexible site structure and that a databse is for you. So what next?

What You Need

If you’re going to build a dynamic site then you’re going to need a few things, namely:

  • a Relational Database Management System (or RDBMS),
  • a programming language that will help you manipulate your data and
  • a template system to abstract presentation from code.

I’ll assume that you know HTML already and that you have some experience of site building which will leave us with these three important choices.

Which RDBMS?

For most of today’s small to medium-sized sites, MySQL is the answer. Why? Because:

  • It’s very powerful
  • It’s very fast
  • It’s very simple
  • It has an almost fanatical online community from which you can draw knowledge and support
  • It’s free!

Which programming language?

On the basis that we’ve chosen MySQL as our database the answer here is a forgone conclusion: PHP. Although there are many other ways to interact with MySQL, the combination of PHP and MySQL is very well documented and thousands of sites rely on this excellent marriage of technologies to efficiently manage their data. Some of PHP’s benefits include:

  • Its simplicity
  • Its power
  • Its community support
  • It’s free!

Which template system?

There are two schools of thought as to which template system is best: PHPLib or FastTemplate. Experienced programmers are divided and both factions are passionate in their debate. I’ve chosen PHPLib because unlike FastTemplate, which was ported from an original Perl script, PHPLib has been exclusively designed for PHP from the ground up.

So our decisions have been made: PHP, MySQL and PHPLib templates.

Where to now?

If you don’t know HTML, leave this article now and come back in a couple of weeks when you’ve got the hang of coding HTML pages by hand. And if you rely on WYSIWYG editors such as Dreamweaver then the same applies: if you’re not comfortable editing code by hand then you need to learn. It’s not hard, trust me.

The easiest way to learn the languages you’ll need in order to database your site is by taking one subject at a time. I’ve found that it’s best done in this order:

  1. Learn PHP
  2. Learn MySQL
  3. Learn to use the PHPLib template system

PHP

Official home of PHP

Downloads

Of course, there are many other ways to download PHP, but these are the easiest solutions for most people.

Setting up

If you decide not to use a ‘ready-to-go’ setup, the PHP manual supplies the best resources for installation on all systems.

The PHP manual is your best bet. Or you can try Kevin Yank’s tutorials, which are part of his new book, including:

There are hundreds of tutorials to chose from on the Web. Simply run a search on Google and pick something relevant to your needs.

Beginners’ tutorials

If you want to get started quickly, the best way is to try a couple of beginners’ tutorials and then move on to the manual and other online subject-specific tutorials.

Of course, there is a range of others but I think these three are probably the best sources for absolute beginners.

General tutorials and sources of knowledge

Again I have to say that there are dozens of online PHP resources but the ones listed above represent the best of what’s out there.

Community forums

These sites are the best places to talk to other developers and get help with stuff that you can’t find, or can’t understand through the manual.

And there you have it! Bookmark this page, go have some fun, and when you’re confidently embedding PHP into you HTML pages, come back and make a start on the next section.

MySQL

Official home of MySQL

This section will be noticeably shorter than the last, as a lot of the resources listed above are also excellent repositories of information on MySQL. Those that are of particular note I’ll repeat but as PHP goes hand in hand with MySQL you should really check out almost all of the previously-mentioned sites.

Downloading

Setting up

Most of the links to PHP resources above also cover MySQL installation as part of the deal. However, here are a couple of MySQL-specific sites:

Beginners’ tutorials

As above, a couple of the more notable or specific resources include:

General tutorials and sources of knowledge

Community forums

Worthwhile resources include:

Well, what are you waiting for? Go learn the MySQL stuff and when you can use and manipulate simple databases with PHP, come back and get stuck into the next section!

The PHPLib Template System

Official home of PHPLib

Hopefully, by this point you’re happily embedding PHP code that pulls data from MySQL directly into your HTML pages. If you find it fun and exciting to do this then great! And if you’ve noticed that it can get a little messy sometimes with all that mixed up HTML and PHP code then you’re going to love PHPLib templates.

The PHPLib template system is actually part of a much larger collection of useful tools for PHP developers, that all come under the heading of PHPLib. For the purpose of this article I’ll simply deal with the template class that can be used as a standalone class, and which you can require() at the top of your pages.

Downloading

This is nice and simple as a class is just a series of functions (or methods) and thus is simply a text file. You’ll need to download the entire PHPLib, though, to extract that file. Download it here.

Once you’ve downloaded the package, unzip it and grab the template.inc file from the php directory.

Setting up

Well, there’s nothing really to set up, just include() or require() it at the top of your pages.

Tutorials

Bearing in mind that a class is just a set of functions you can use like any other pre-built or user-defined functions in PHP, there are only a few tutorials available. The two listed below are both great, and provide an excellent starting point.

Community forums

Your best bet here is to post questions on PHPbuilder.com, as there are quite a few hardcore template fans that are always happy to lend a hand on this particular forum. Just remember to put a clear subject heading on your post. Something like ‘PHPLib template query’ should do the trick.

Well, that’s all folks, I hope you found this article useful and that you will enjoy designing your dynamic pages using PHP, MySQL and the PHPLib template class.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments