Here is the Data class. It may run out to the right a bit.
<?php
/*
##################################################
#
# Filename..........: $RCSfile: Data.php,v $
# Original Author...: Anthony L. Awtrey
# Version...........: $Revision: 0.1 $
# Last Modified By..: $Author: aawtrey $
# Last Modified.....: $Date: 2006/09/21 18:15:56 $
#
# Copyright 2006 Anthony Awtrey
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
*/
/*
* Requires the ADODB database abstraction package for PHP5. This constant is
* defined in the 'master.php' file.
*/
require_once(ADODB_PATH);
/*
* This is a simple wrapper object which allows us to interact with a given
* table in a database. We use adodb for abstraction, but there are still
* probably MySQL-ism around here.
*/
class Data {
var $db_type = 'mysql';
var $db_host_name = DB_HOST_NAME; # Defined in 'master.php' file
var $db_user_name = DB_USER_NAME; # Defined in 'master.php' file
var $db_user_pass = DB_USER_PASS; # Defined in 'master.php' file
var $db_name = DB_NAME; # Defined in 'master.php' file
var $table_name;
var $id;
var $attr = array();
var $db;
/*
* Class initializer
*/
function __construct($table_name) {
$this->table_name = $table_name;
$this->db = &ADONewConnection($this->db_type);
$result = $this->db->Pconnect($this->db_host_name,$this->db_user_name,$this->db_user_pass,$this->db_name);
}
/*
* Turns on debug messages
*/
function debug_on() {
$this->db->debug = true;
}
/*
* Turns off debug messages
*/
function debug_off() {
$this->db->debug = false;
}
/*
* Sets a key -> value pair representing an aspect of the current record
*/
function set_attribute($attribute, $value) {
if ( $attribute == 'id' ) {
$this->id = $value;
} else {
$this->attr[$attribute] = $value;
}
}
/*
* Commit changes to the database
*/
function execute($sql) {
$results = $this->db->Execute($sql);
return $results;
}
/*
* Get a record matching a given id or return a blank record. Used by save() method.
*/
function get_recordset($id='') {
if ( $id == '' ) {
$sql = "SELECT * FROM " . $this->table_name . " WHERE (id=-1)";
} else {
$sql = "SELECT * FROM " . $this->table_name . " WHERE (id=$id)";
}
$results = $this->db->Execute($sql);
return $results;
}
/*
* Get the results of a SQL statement as a simple array
*/
function return_array($sql) {
global $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$results = $this->db->Execute($sql);
if ($results) {
return $results->GetArray();
} else {
return false;
}
}
/*
* Get the results of a SQL statement as an associative array
*/
function return_assoc($sql) {
global $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$results = $this->db->Execute($sql);
if ($results) {
return $results->GetAssoc();
} else {
return false;
}
}
/*
* Returns the count of records in a table with optional where clause limit
*/
function count_records($field='',$value='') {
$sql = "SELECT COUNT(*) FROM " . $this->table_name;
if ($field != '' && $value != '') {
if (@preg_match("/^[0-9]+$/",$value)) {
$sql .= " WHERE ($field=$value)";
} else {
$sql .= " WHERE ($field='$value')";
}
}
$results = $this->db->Execute($sql);
$results = $results->GetRows();
return $results[0]['COUNT(*)'];
}
/*
* Return a single record (as opposed to set of records) by id as an
* associative array. Used in the login function in 'master.php' file.
*/
function get_record_by_id($id) {
$sql = "SELECT * FROM " . $this->table_name . " WHERE (id=$id)";
$results = $this->return_array($sql);
return $results[0];
}
/*
* Returns a single record with optional where clause limit
*/
function get_record($field='',$value='') {
global $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$sql = "SELECT * FROM " . $this->table_name;
if ($field != '' && $value != '') {
if (@preg_match("/^[0-9]+$/",$value)) {
$sql .= " WHERE ($field=$value)";
} else {
$sql .= " WHERE ($field='$value')";
}
}
$results = $this->return_array($sql);
return $results[0];
}
/*
* Returns an array of records with optional where clause limit
*/
function get_records($field='',$value='',$order='') {
$sql = "SELECT * FROM " . $this->table_name;
if ($field != '' && $value != '') {
if (@preg_match("/^[0-9]+$/",$value)) {
$sql .= " WHERE ($field=$value)";
} else {
$sql .= " WHERE ($field='$value')";
}
}
if ($order != '') {
$sql .= " ORDER BY $order";
}
$results = $this->return_array($sql);
return $results;
}
/*
* Returns an associative array of records with optional where clause limit
*/
function get_records_assoc($field='',$value='',$order='') {
$sql = "SELECT * FROM " . $this->table_name;
if ($field != '' && $value != '') {
if (@preg_match("/^[0-9]+$/",$value)) {
$sql .= " WHERE ($field=$value)";
} else {
$sql .= " WHERE ($field='$value')";
}
}
if ($order != '') {
$sql .= " ORDER BY $order";
}
$results = $this->return_assoc($sql);
return $results;
}
/*
* Returns an array with a where clause that calculates based on year for
* the given time/date field in the database
*/
function get_records_by_year($field='',$year='') {
$sql = "SELECT * FROM " . $this->table_name;
$sql .= " WHERE (EXTRACT(YEAR FROM $field)=$year)";
$sql .= " ORDER BY $field DESC";
$results = $this->return_array($sql);
return $results;
}
/*
* Saves the data in the current recordset
*/
function save() {
if ($this->id) {
$rs = $this->get_recordset($this->id);
$sql = $this->db->GetUpdateSQL($rs, $this->attr);
} else {
$rs = $this->get_recordset();
$sql = $this->db->GetInsertSQL($rs, $this->attr);
}
if ($sql) {
$results = $this->db->Execute($sql);
return $results;
}
return true;
}
/*
* Deletes a record by id
*/
function delete_record_by_id($id) {
$sql = "DELETE FROM " . $this->table_name . " WHERE (id=$id)";
$result = $this->db->execute($sql);
if ($result) {
return true;
} else {
return $result;
}
}
/*
* Deletes records based on where clause matching
*/
function delete_records($field,$value) {
$sql = "DELETE FROM " . $this->table_name . "";
if ($field != '' && $value != '') {
if (@preg_match("/^[0-9]+$/",$value)) {
$sql .= " WHERE ($field=$value)";
} else {
$sql .= " WHERE ($field='$value')";
}
}
$result = $this->db->execute($sql);
if ($result) {
return true;
} else {
return $result;
}
}
}
?>