Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

For the efficiency-minded, this use of recursion is not optimal. Each thread will cause
another call to showMessages, which causes another query to the database. There is a
way to query the database once and traverse the tree of messages in memory, but I'll
leave that as exercise for you.


If a message title is clicked on, the page is reloaded with messageID set. This causes
the script to switch over into the mode where a message is displayed. The fields of the
message are displayed in a table. If the message contains any HTML, it will be rendered
by the browser, because no attempt is made to filter it out. This restriction is best applied
as part of the code that adds a new message.


Regardless of the two modes, a form is shown for adding a message. If a message is
added while the list of messages is shown, the message will be added to the root level. If
a message is added while the user is viewing a message, then it will be considered a
reply. The new message will be made a child of the viewed message.


This BBS is simple, but the core functionality exists. A more sophisticated solution might
involve allowing only authenticated users to add messages, or keeping messages private
until approved by a moderator. You can use this same structure to build any application
that manages user-submitted data, such as a guest book. If you are searching for a
sophisticated BBS solution, I suggest checking out Brian Moon's Phorum project
http://www.phorum.org/.


Database Abstraction Layers


Imagine creating a Web application that uses MySQL and later being asked to make it
work with Oracle. All the PHP functions are different, so you'd have to change every one.
In addition, as MySQL and Oracle each use slightly different SQL, you will probably
have to change most of your queries. One solution to this problem is adding an
abstraction layer. This separates your business logic—the rules of your application—
from the code that interfaces with the database. A single function calls the right function
based on the type of database you need to query.


Perhaps the most popular database abstraction layer is part of the PHP Base Library
<http://phplib.netuse.de/ >. This library also contains code for session
management. Another abstraction layer is Metabase, available at the PHP Classes
Repository <http://phpclasses.upperdesign.com/ >.


Despite abstraction layers, incompatibilities between databases continue to offer
challenges. MySQL uses a special qualifier for column definitions called
AUTO_INCREMENT. It causes a column to be populated automatically with integers in
ascending order. In Oracle this functionality can be approximated using a sequence and a
trigger. The differences are difficult to reconcile systematically. In 1999, Scott Ambler

Free download pdf