Building Database-Driven Applications with PHP and MySQL: Part II, Page 4
The Front End – Article Display
The final part of this application is the front end. As is the case with most applications, building this part is significantly less involved than the back end. Most of the programming involves grabbing the news items from the database. The rest involves designing how the information should be laid out. Take a look at the script:
In many respects, this script is identical to the administrative article listing. Here, too, we are retrieving all of the articles from the database and outputting them. This time though, all the columns in the table are retrieved. We also add an ORDER BY clause to the query so the latest article is listed first; MySQL will sort on the date column here.
Within the loop, there is some additional formatting of various fields. Because the date was stored as a Unix timestamp, it needs to be converted to something that's understandable. PHP's date() function is used to change it to "Day Month, Year" format. We also run nl2br() on the article body, so that any line breaks entered when the article was created are preserved as HTML <br> tags.
Whether it is a guestbook for a small site or a shopping cart with a 500 product inventory, every dynamic application is composed of the same basic parts. Admin pages, which are typically password protected, allow content to be added to a database, edited and deleted and a front end accessible to all users organizes, which assembles and displays the content in a meaningful way.
In this article you've learned how to use the basics of PHP/MySQL interaction to develop a complete application which incorporates all of these elements.
The next article will revisit the news application; you'll learn how to expand it using some of MySQL more advanced features.