CartMetrix - Do you know yours?

« French Family Values | Home | Logging and Monitoring Apache »

8/4/2005

An introduction to Service Data Objects for PHP

This is the way programming should be done. Unfortunately, in the real world few have time to build apps this way. Starting from scratch maybe, but working on code day to day especially picking up from another coder?

IBM Developerworks - An introduction to Service Data Objects for PHP

The thing I do use in everyday coding are data access functions.

<?php
function get_customer_record($id, $format=‘detail’) {
   global $dbh;

   $dbh->Query(“SELECT * FROM customers cust, address addr
         WHERE customer_id = ‘$id’
         AND cust.id = addr.customer_id”
);
   
   if($dbh->NumRows < 1)   return false;
   $customer = $dbh->FetchObject();
   
   switch($format)   {
      default:
      case(‘db’):
         //do nothing just return customer object
         break;
         
      case(‘list’):
      case(‘detail’):
         format_customer_record($id, $format);
         break;
   }
   
   return $customer;

}

function format_customer_record($customer, $format=‘detail’)   {

   switch($format)   {
      case(‘detail’):
         //print out entire record
         break;
         
      case(‘list’):
         //print out minimal record for row listing
         break;
   }

   return true;
}
?>

Starting with these simple functions, you have access to the data and output mechanisms that can easily grow as your application design needs do. You can add new case statements to the formatting for use of the data in different ways or get the raw data and manipulate it inline for single use routines.

Popularity: 8%

Trackback:

Related Posts

Post your opinion

Verification Image

Please type the letters you see in the picture.

Subscribe without commenting


damonparker.org is proudly powered by WordPress
Entries (RSS) and Comments (RSS).

copyright © 2002-2008 damonparker.org. all rights reserved.

Close
E-mail It