Now, I can implement this application using a variety of different scripting languages and technologies. I can use Perl, PHP or JSP. I can store the data in a database, in an ASCII text file, or in an XML-encoded document. My personal preference here is PHP for the scripting, and a MySQL database for the data storage - first, because I find development much faster with PHP (and I also happen to like the language a great deal), and second, because the customer's existing Web site is also built on PHP and already has an active database server. Before reading further, you should download the source code for this case study, so that you can refer to it throughout this article (you will need a Web server capable of running PHP and a mySQL database). Got it? Good. Now, the first thing I'm going to do is design the database tables that will hold the news information, using the information provided to me on the attributes of a press release. Here's what I came up with: As you can see, this maps right into the information on the previous page - I have one field for every element of a press release. For my initial development, I'm going to populate this table with a couple of dummy records. Here goes: Right. Let's get on with some code.{mospagebreak title=The Simple Stuff} You'll remember, from the requirements discussion a couple pages back, that this development effort can broadly be split into two parts. One part consists of the scripts that retrieve the list of newest items from the database and display this list to the user; the other consists of administration scripts that allow editors to manage this list, enter new information, and edit or delete existing information. Since the first part is simpler, I'm going to get that out of the way first. The scripts involved in this are: "list.php" - the script which retrieves a list of the five newest entries in the database; "story.php" - the script which displays the full text for the selected story. These scripts are stored within the "user" directory in the source code archive. Here's my first script, which displays the list of five most recent press releases from the database. There's no magic here at all. This script simply connects to the database, retrieves a set of records, and formats them for display in a Web browser. Let's look at it in detail.{mospagebreak title=An Anatomical Exploration} The first step is, obviously, to open a connection to the database through which queries can be transmitted - this is accomplished via PHP's mysql_connect() function: In case you're wondering, the host name, user name and password are all variables sourced from the configuration file "conf.php". This file has been include() at the top of the script, and it looks like this: Extracting this configuration information into a separate file makes it easier to update the application in case the database username or password changes. It's far easier to update a single file than it is to update multiple scripts, each with the values hard-wired into it. Next, a database needs to be selected, via the mysql_select_db() function: Again, the database name comes from the configuration file "conf.php". With the connection open, the next step is to execute an SQL query, via the mysql_query() function: Note the addition of the DESC clause in the SELECT statement above - this orders the items in the order of most recent first - and the additional LIMIT clause, which restricts the resultset to five items only. Once a resultset has been returned, I've used the mysql_fetch_object() function, in combination with a "while" loop, to iterate through it and format the fields in each row for display. In case you're wondering, the mysql_fetch_object() function is a nifty little thing I only discovered recently. It converts each row into a PHP object, and represents each column within that row as a property of that object; field values can then be accessed in standard object notation. Therefore, in order to access the column named "slug" of each row, I would simply use the code The formatDate() function you see in the code above is a function I wrote to turn a MySQL timestamp into a human-friendly date string. Here's the function definition: Note the addition of several lines of code that tell the script what to do if no records are returned by the query. If I didn't have this, and the database turned out to be empty (which might happen the very first time the application was installed for use), the generated page would be completely empty - not a very nice thing to show to your users, especially on a potentially high-traffic page. So I've worked around this with some code that displays a neat little message if no records are found in the database. It's important, as a developer, to think through all possible situations and write code that handles each one intelligently. The possibility of an empty database doesn't even occur to many novice developers - and this can lead to embarrassing situations if you're demonstrating the application to your boss...or worse, the customer. Finally, with all the records processed and displayed, the mysql_close() function terminates the database connection. Here's what it all looks like: ![]()
blog comments powered by Disqus |