Dynamic HTML: A Practical and Simple Solution for a Database Driven Website

Dynamic HTML: A Practical and Simple Solution for a Database Driven Website
Page content

Why Generate Dynamic HTML Code?

Many programming and scripting languages are widely used in web development; however, good ol’ hypertext mark-up language (HTML) or better yet, extensible hypertext mark-up language (X)HTML still has its place. The database driven website dominates the world wide web (WWW) nowadays and this type of site is probably one of the best examples of just why it’s so important for web developers to know how to generate dynamic HTML. Imagine hundreds, even thousands of articles available at a website, each one of them needing a unique title tag, keywords, and a meta description in order to be found by the search engine robots. It would not be realistic for even a large team of web developers to attempt to manually provide such information in the head of the page for each article. Fortunately, the combination of HTML and PHP (hypertext preprocessor language) can serve to generate dynamic HTML. When I was first introduced to programming in PHP, the concept of generating HTML with PHP didn’t make sense to me until I actually began to use the two languages together and see the power of PHP.

Using HTML and PHP to Your Advantage: Explanation of Real-World Code

Below is the screenshot of the actual real-world code that I’ve used to generate a dynamic title tag for a database driven website. You can easily adapt it to fit your site or your client’s needs if you already possess a sound knowledge of HTML, PHP, the basics of the SQL language, and how to “slice” a website into its parts: the head or header; the body, and the foot or footer. This snippet of code to generate dynamic HTML code is, of course, placed in the head/header.

Line 1. I simply comment what the block of code to follow is to accomplish.

Line 2. This is just the opening PHP tag.

Line 3. This is my query; of course, you’ll need to modify the code to work with the database that’s driving your site–in other words, your different table and column names.

Line 4. This is the variable to hold the results of the query.

Line 5. This is the variable for the running of the query.

Line 6. Here begins the if…else statement. This is a set-up for a static and generic HTML title tag to be generated if a visitor lands on or goes directly to the home page or any other page that doesn’t display a specific article in its fullness. This generic title covers the specialization or theme of the entire database driven website. If, however, the visitor lands on or goes to a specific article, a dynamic HTML title tag that is for that particular article is generated by being “called” from the title stored in the database.

Line 7. The echo statement to execute for the visitation of any page that isn’t a specific article.

Line 8. This is the end of the if statement.

Line 10. This is just a comment. Again, commenting code is important to keep track of what’s going on. Line nine is blank.

Line 11. This is the else part of the if…else construction that will execute if a particular article page is being visited. Lines 11 and 12 are the most important for the generation of dynamic HTML.

Line 12. This is the echo statement to print the dynamic title tag. Notice that I chose to “tack” the generic title on to the dynamic one. You don’t have to do this.

Line 16. Closing PHP tag.