Home arrow PHP arrow Page 3 - Databases: Finishing a Listing Service

Displaying the Database - PHP

Concluding our discussion of databases and PHP, we'll finish building the example that we started last week. This article is excerpted from chapter eight of the book Programming PHP, Second Edition, written by Kevin Tatroe, Rasmus Lerdorf, and Peter MacIntyre (O'Reilly, 2006; ISBN: 0596006810). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

TABLE OF CONTENTS:
  1. Databases: Finishing a Listing Service
  2. Adding a Business
  3. Displaying the Database
  4. PHP Data Objects
  5. PDO and prepared statements
By: O'Reilly Media
Rating: starstarstarstarstar / 2
July 05, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Example 8-6 shows a page that displays the information in the database. The links on the left side of the page are created from the categories table and link back to the script, adding a category ID. The category ID forms the basis for a query on the businesses table and the biz_categories table.

Example 8-6.  Business listing page

<html>
<head>
<title>
<?php
 
$doc_title = 'Business Listings';
 
echo "$doc_title\n";
?>
</title>
</head>
<body>
<h1>
<?= $doc_title ?>
</h1>

<?php
 // establish the database connection

 require_once('db_login.php');

 $pick_message = 'Click on a category to find business listings:';
?>

<table border=0>
<tr><td valign="top">
    <table border=5>
    <tr><td class="picklist"><strong><?= $pick_message ?></strong></td></tr>
   
<p>
   
<?php
     // build the scrolling pick list for the categories
     $sql = "SELECT * FROM categories";
     $result = $db->query($sql);
     if (DB::isError($result)) die($result->getMessage());
     while ($row = $result->fetchRow()){
         if (DB::isError($row)) die($row->getMessage());
         echo '<tr><td class="formlabel">';
         echo "<a href="$PHP_SELF?cat_id=$row[0]">";
         echo "$row[1]</a></td></tr>\n";
     }
    ?>
    </table>
</td>
<td valign="top">
     <table border=1>
     <?php

     if ($cat_id) {
       $sql = "SELECT * FROM businesses b, biz_categories bc where";
       $sql .= " category_id = '$cat_id'";
       $sql .= " and b.business_id = bc.business_id";
       $result = $db->query($sql);
       if (DB::isError($result)) die($result->getMessage());
       while ($row = $result->fetchRow()){
        
if (DB::isError($row)) die($row->getMessage());
        
if ($color == 1) {
           $bg_shade = 'dark';
           $color = 0;
         } else {
           $bg_shade = 'light';
           $color = 1;
        
}
         echo "<tr>\n";
         for($i = 0; $i < count($row); $i++) {
           echo "<td class="$bg_shade">$row[$i]</td>\n";
         }
         echo "</tr>\n";
     
}
    }
   ?>
   </table>
</td></tr>
</table>
</body>
</html>

The business listings page is illustrated in Figure 8-7.


Figure 8-7.  Business Listings page



 
 
>>> More PHP Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: