Tie it together
The preceding functions provide a nice facility to design any site layout. This example creates a rather simple layout where the news links are shown in a left column and the main site content takes up the rest of the space, as shown in Figure 2.
Take a look at the code that generates this layout. I think you'll be surprised at how little code is needed, once most of the features are available as functions. Listing 4 contains the details.Listing 4. Defining the main site
The code in Listing 4 checks to see whether the
is set to 'logout'. If so, the
logout function is called, which
unregisters all session variables so that the site will consider this user
logged out. Next, the code calls the
site_header function to print
out the site template and pass it the string 'Home' to be displayed in
Now it's time to select the news items that the visitor will see. The code
logged_in function to parse the visitor's preferences string.
For each character in the string set to 1, news from one source is displayed.
First, the code calls the
box_top function (defined
site.php) to print out the necessary table elements to display
a box around the source, with the news source's name and home page URL
already passed into the function to be shown in the box header. Next, the
code calls the
show_news function to display the news items for
that news source. Last, it calls
box_bottom to close off the box
boundary. If a site visitor is not logged in, the code will simply query
the database for all news sources and display boxes for all of them.
After this point, the PHP script is basically done, and you can add the actual page content. More than likely, a site would have a separate database for its own content and you could write more PHP code to grab the content from that database.
Breaking code down into functions lets you create structures that are reusable anywhere on a Web site. You can then create any page layout easily. It should be very simple to add a right menu containing more news links. Alternatively, the entire page could be a large table of news boxes and with added code so that each visitor could select the position of each box within a row or column. You could add other boxes to show information such as the weather forecast, horoscopes, television schedules, or event reminders. Any of these would simply be an addition to the preferences page. Just remember to encapsulate each new feature so it can be used elsewhere. Most of all, be creative and have fun doing it.