August 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Building Database-Driven Applications with PHP and MySQL: Part II

  • August 25, 2003
  • By Elizabeth Fulghum
  • Send Email »
  • More Articles »

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:

index.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>News Articles</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
td,body {
   font-family: Arial, Helvetica, sans-serif;
   font-size: 12px;
}
-->
</style>
</head>
<body>

<?php
//include global file, establishes connection to database
include 'global.php';

$result = mysql_query("SELECT * FROM news ORDER BY date DESC");

//output each article
while($row=mysql_fetch_array($result)) {
   print '<h3>'.$row['title'].' - '.date("F j, Y", $row['date']).'</h3><p><b>Author:</b> '.$row['author'].'</p><p>'.nl2br($row['article']).'</p>';
}
?>

</table>
</body>
</html>

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.


Finally

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.

Stay Tuned!





Page 4 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel