PHP Classes

File: DB_CountryCodes.php

Recommend this page to a friend!
  Classes of Kevin Hagel   DB Country Codes   DB_CountryCodes.php   Download  
File: DB_CountryCodes.php
Role: Class source
Content type: text/plain
Description: create/maintain DB of country codes
Class: DB Country Codes
Store and retrieve country information in database
Author: By
Last change: Added codenamehash() function, returns a "code" => "name" hash of all country codes.
Date: 20 years ago
Size: 4,937 bytes
 

Contents

Class file image Download
<?php
/**
 * @name DB_CountryCodes
 * @package IANATools
 * @author Kevin Hagel <khote@mminternet.com>
 * @version $Revision: 1.0 $
 * @since April 27, 2004
 * @requires ianacclister, PEAR, DB, DB_Table
 *
 * Country Codes Accessor, creator, etc. More in my IANATools package, this
 * class extends DB_Table to allow you to create, insert data, update data etc,
 * using the dbms's supported by DB_Table.
 *
 * Uses ianacclister on initdb to
 *
 * Assuming a PEAR_DB $db, To create the database, and initialize it:
 *
 * $table = "countrycodes";
 * $auto_create = 'drop';
 * $cc =& new CountryCodes($db,$table,$auto_create);
 * $result =& $cc->initdb();
 *
 * And of course to update. Note the 'safe' option for $auto_create
 *
 * $table = "countrycodes";
 * $auto_create = 'safe';
 * $cc =& new DB_CountryCodes($db,$table,$auto_create);
 * $result =& $cc->updatedb();
 *
 * Accessing DB Contents:
 *
 * $table = "countrycodes";
 * $auto_create = 'safe';
 * $cc =& new DB_CountryCodes($db,$table,$auto_create);
 *
 * $rows =& $cc->listall();
 * $assoc =& $cc->listassoc();
 * $record =& $cc->getRecord();
*/
require_once "DB.php";
require_once
"DB/Table.php";
require_once
'class.ianacclister.php';


class
DB_CountryCodes extends DB_Table
{
   
// Describe the columns
   
var $col = array(
       
'code' => array('type' => 'char', 'require' => true, 'size' => 2 ),
       
'name' => array('type' => 'varchar', 'require' => true, 'size' => 128 ),
       
'whois' => array('type' => 'varchar', 'require' => true, 'size' => 128 ),
       
'extra' => array('type' => 'clob', 'require' => false, 'size' => 256 )
    );

   
// Use the country-code for the primary key.
   
var $idx = array(
       
'code' => 'unique',
       
'name' => 'normal',
    );

   
// SQL we use
    // listall - dumps all records
    // listassoc - dumps all records, keyed by code
   
var $sql = array(
       
'listall' => array('order' => 'code', 'get' => 'all'),
       
'listassoc' => array('order' => 'code', 'get' => 'assoc'),
       
'getrecord' => array('select' => 'name, whois, extra', 'get' => 'row'),
       
'codenamerows' => array('select' => 'code,name', 'get' => 'assoc'),
    );

   
/**
     * Initialize the db with ianacclister data
     * @return mixed void on success, PEAR_Error on failure.
    */
   
function initdb()
    {
       
$hash =& ianacclister::getHash();
        foreach (
$hash as $blah=>$data) {
           
$cols_vals = array(
               
'code' => $data['code'],
               
'name' => $data['name'],
               
'whois' => $data['whois'],
               
'extra' => "Used for extra data ..."
           
);
           
$result = $this->insert($cols_vals);
            if (
PEAR::isError($result)) {
                return
$result;
            }
        }
        return
$result; // Return whatever was the last result
   
}

   
/**
     * Update the database
     * @return mixed void on success, PEAR_Error on failure.
    */
   
function updatedb()
    {
       
$hash =& ianacclister::getHash();
        foreach (
$hash as $blah=>$data) {
           
$cols_vals = array(
               
'name' => $data['name'],
               
'whois' => $data['whois'],
               
'extra' => "blah for now"
           
);
           
$where = "code = '".$data['code']."'";
           
$result = $this->update($cols_vals,$where);
            if (
PEAR::isError($result)) {
                return
$result;
            }
        }
        return
$result; // Return whatever was the last result
   
}


   
//------------------------\\
    //----- Accessors --------||
    //------------------------//


    /**
     * List everything, do a SELECT *
     * @return array of records on success, PEAR_Error on failure.
    */
   
function &listall()
    {
       
$rows =& $this->select("listall");
        return
$rows;
    }

   
/**
     * List everything, do a SELECT *, return in assoc array keyed by ccode.
     * @return code-keyed assoc array on success, PEAR_Error on failure.
    */
   
function &listassoc()
    {
       
$rows =& $this->select("listassoc");
        return
$rows;
    }

   
/**
     * returns a hash of "code" => "name", which is probably used
     * more than the others.
    */
   
function &codenamehash()
    {
       
$rows =& $this->select("codenamerows");
        return
$rows;
    }

   
/**
     * get a particular record
     * @return array containing country code data if found, NULL if not found,
     * and PEAR_Error on error.
    */
   
function &getRecord($code=NULL)
    {
        if(empty(
$code)) {
            return new
PEAR_Error("Invalid Argument: code received is empty");
        }
       
$view = "getrecord";
       
$filter = "code = '$code'";
        return
$this->select($view, $filter);
    }

}
?>