Free Programming Books
Free download ebooks on computer and programming

Free PHP/MySQL ebook "PHP MySQL Website Programming: Problem - Design - Solution" Sample Chapter

PHP MySQL Web Programming...
Free download Chapter 14: The Road Ahead
Download chapter

PHP MySQL Website Programming: Problem - Design - Solution shows the development process for a website using a specific set of technologies: the Apache web server, the MySQL database system, and the PHP scripting language. It gives you a completely hands-on experience and guides you through the construction of a complete application-driven site from design to deployment.

Each chapter in the book is broken into three parts:

  • Problem: The authors analyze each task and identify areas that may be particularly hard to implement.
  • Design: Once the problems have been identified, a proposal is sketched out for solving them.
  • Solution: The code is developed and deployed to accomplish the task at hand.
  • Throughout the book, good object-oriented PHP coding methods are used where appropriate. The site you construct features a modular design, so the individual chapters are generally focused on designing and building a specific piece of the site's functionality. You can find a working model of the site online at http://apress.mediatemple.net .

This invaluable tutorial provides a lot of insight into the ways you can implement web applications with PHP and MySQL in real-world situations.

< < prev next > >

Looking for More Flexibility in Look and Feel

In Chapter 3 we used Cascading Style Sheets (CSS) in our header and footer code. This served us very well throughout the book, and is likely to work well in most simple situations. But what if you wanted to provide a different look and feel for affiliate DVD shops, so they could use your online shop, but with their own logos, layout, and color scheme?

Well, you could go down the road of writing an individual template file for each affiliate, but over time this broken up HTML and PHP file is very difficult to maintain, since you cannot instantly get an idea of what the site will look like when editing the HTML.

This is where templating in PHP comes in - although PHP really started off as a way to add 'variables' to HTML, it's now far more than that, as you can see from the site we have developed. Templating solutions were developed to try and solve the issue of mixing PHP and HTML. The great advantage of this is that you can design your web pages in WYSISYG tools like Macromedia Dreamweaver, which also allows you preview PHP pages live on a development server. This static HTML can then be easily integrated with the web site's PHP code.

While there is not enough space in this book to cover all of the available packages, or even tell you about the detailed benefits of each, we will provide a general overview of the two main types and their advantages and disadvantages.

For a more detailed discussion of PHP templating, look out for the PHP Templating Handbook (ISBN 1-861008-96-1) currently under development by this publisher.

HTML Tag Replacers

In the HTML file, we can put in comments and tags indicating the start and end of blocks or where to show variables:


<!-- BLOCK: START someloop -->

<td>{somevariable}</td>
These engines are usually very easy to write templates for in WYSISYG editors, and the person designing the HTML page does not require any real knowledge of writing PHP applications.

A lot of the work is done by the PHP program when displaying the page by replacing the tags with PHP variables or values, often using the PHP's str_replace function. For simple tasks this type of template is very easy to use; however, it can become difficult to work with if you have many complex loops and other programming blocks. You should also be aware that if performance is a concern, some packages that use this method can be very slow.

PHP Code Replacers

The second approach for templates is to take a template (similar to the one we saw earlier) and replace it with PHP code:


<?php foreach($someblock as $somevariable) { ?>

<td><?php echo $somevariable ?></td>

<?php } ?>
This method is significant in terms of performance, since the file is converted only when it's modified, and can allow template engines to have considerably more features.

Hopefully this gives you a start on understanding templates and how they could be implemented with our DVD site; you will be seeing more of this in the next book on e-commerce.

Other than these resources, you may want to explore XSLT in the PHP4 XML book from Apress (ISBN 1-861007-21-3). As is often the case, there is no single 'right' way to do this, just a lot of excellent alternatives for you to make your own choice based on your circumstances and requirements: