Today, Web sites strive to deliver the ultimate user experience. Beyond user-friendliness, good service, and meaningful information, custom and dynamic content can improve the usefulness of your site and enhance visitor functions, making users more likely to return in the future. In this article, we'll start with an overview of dynamic information. Then, we'll explain how to use PHP to create dynamic content within a Web page and look at a sample script that demonstrates the process.
Dynamic information theory
According to Merriam-Webster Online, dynamic means "marked by usually continuous and productive activity or change." So when we talk about dynamic data, we mean that the information to be sent to the client as a Web page is a variable compilation of source data. This contrasts with static Web pages, which are made up of content that is not dependent on variable input and that are generally parsed directly to the user. There are three main types of dynamic information on the Web:
- Dynamic data—Variables within a Web page are generated.
- Dynamic Web pages—An entire Web page is generated.
- Dynamic content—Portions of a Web page are generated.
The more granular control you want to have, as with dynamic data, the more complicated the data handling will be. And the greater the scope of the information you want to generate, as with dynamic Web pages, the more complicated the logic will be. Dynamic content is a happy medium between the two and gives us an opportunity to look at two very useful PHP functions, include() and require().
Remember, the more cranking you have to do on the back end, the bigger the performance hit your site will experience. Fortunately, PHP does a good job of streamlining the preprocessing time, so I try to use PHP's functions as much as possible when dealing with dynamic content and data.
Data sources and PHP functions
All dynamic content has one thing in common: It comes from a data source outside the originating page. Figure A lists some common data sources and the PHP functions that handle them.
In the article "Tutorial: Getting started with PHP," we looked at a sample script that asked users to enter their favorite number. Based on the results, we displayed a message. That is a simple example of user-driven dynamic content. The results from a Web form are used to determine what content to show. A more sophisticated example would be a "click-stream" application that is used to determine what ads to show a user based on which pages he or she has visited within the site.
Once data is entered, by a user or otherwise, it is stored in a database and then recalled. If it is used to determine what content to show, it is considered to be "database-driven dynamic content." We'll take a closer look at this type of dynamic information in our next article.
For now, let's look at an example of file-driven dynamic content using a simple script. We will be using logic based on a configuration file to determine what body style and font to show on the Web page. The chosen style will then be displayed when our Web page is requested. (A note about the example include files: You really should use style sheets for functionality like that used in the example.)
Sample script: Display.php
The Display.php script uses a separate configuration file containing variable values and several include files that contain the variable portion of the HTML. While this may not seem very dynamic, you could easily ask the user to create the configuration file with a Web form and use logic to determine which configuration file to load, etc. (The techniques we discuss in "Understanding functions and classes in PHP" will help you accomplish this.)
For our purposes here, we'll skip that aspect of the process and keep it simple. Listing A shows our main script, and the page you would call in your browser, Display.php. (PHP code appears in bold.)
This is a Web page whose style is determined by a configuration file. —>
$required_file = $display.".php";
<center>This is the best "mood page" ever!</center>
This simple code must do three things:
- Use the PHP include() function to include and evaluate variables in the file Displayconf.php.
- Create a variable representing the name of the file to be required. In our case, the variable $display, which is defined in the Displayconf.php file, is evaluated and then appended with .php. (This is our logic step.)
- Use the PHP require() function to display the content from the appropriate included file.
Note that in our example, the PHP require() and include() functions are completely interchangeable. The main difference between these two functions is in how the target files are handled. A require() statement will be replaced by the file it has called. That means that the remote file is literally called only once, in the case of a loop. The include() function, on the other hand, will be evaluated every time it is encountered. This means that in a loop, the file will be accessed each time, and any variables that are set in the included file will be updated.
In the example, I have tried to give an indication of when it is appropriate to call each function. In the case of the file Displayconf.php, it's likely that values within it have changed. It is, after all, a configuration file. Therefore, I have opted to use the include() function. On the other hand, the file $required_file will most likely not change on the fly. If different body tags are required, a new file will probably be created for inclusion, so I used the require() function.
Advanced users may want to review the PHP manual regarding the functions require_once() and include_once() for even better control of file handling and configuration file variable management.
Listing B shows our configuration file, Displayconf.php. (For the sake of simplicity, we'll put all the files in the same directory on our Web server.) All we do here is set the variable $display to one of the optional values.
# Configuration file for display.php
# Set the variable $display to one of the following:
# happy, sad, or generic
$display = "happy";
Finally, we need some content files—one for each option in the configuration file. Since the content is static HTML, we don't need the PHP ear tags in the file. When you use the include() or require() functions in PHP, the affected file is escaped out of PHP at the beginning and back into it at the end.
"Happy" file content (happy.php)
<body bgcolor=pink text=yellow>
"Sad" file content (sad.php)
<body bgcolor=blue text=white>
<font face="arial, helvetica" size="+5">
"Generic" file content (generic.php)
<body bgcolor=white text=black>
<font face="courier" size="+5">
When you hit the page, Display.php, the look and feel should change based on the value you entered into the configuration file.
In this article, we discussed the basics of dynamic information and used a script to create file-driven dynamic content. Specifically, we used the include() and require() PHP functions to drive our data.
Here are a couple of final notes. While I'm sure you're familiar with the WAI Web Coding Guidelines, it may be a good idea to review what the W3C has to say regarding dynamic content and user accessibility to it. You may also want to check out the PHP manual chapter "Using Remote Files" for more information on pulling configuration data using FTP.
Our next article will focus on database-driven content. We'll talk about connecting to a database and manipulating it, and we'll look at a more complicated example of dynamic content.
What's so hard about dynamic content?
What challenges have you faced working with dynamic content? Do you have suggestions for others facing the same problems? Send us an e-mail with your experiences and suggestions or post a comment below.