quote: "Mom said never to use an Ak-47 against the fish man!"
BastardDomain Forums \ Development \ php code test
php code test
Fox
The Guzzler

avatar Fox

Registered: 10 Apr 2001
Location: North-Brabant
Posts: 9399
8 Nov 2006

<?php
include_once("module.functions.php"smilie;
/**
* This class is responsible a base class
* The functions provided are the folowing:
*
* add() adds an item to the database
* modify() modifies an item item in the database
* delete() deletes an item from the database
* get() gets an item from the database
* list() lists all the items from the database
* hit() increases the number of hits associated with an item by one
* install() creates a MySQL table
* exists() true if the MySQL table already exists
*
* @author Gerben Kegel
* @version 1.0
* @modified 2006-11-07
*/
class Table {
var $q; // the number of queries made
var $field; // an array that contains the fields of the table
var $table; // name of the table
var $standard_field; // an array of standard field which exists in every table
var $standard_type; // an array of the types cooresponding to the standard fields

/**
* This is the constructor. It verifies a database connection.
*/
function Table($name, $field) {
include_once("module.connect.php"smilie;
$this->q = &$GLOBALS['query'];
$this->field = $field;
//$this->type = $type;
$this->table = $name;
$this->standard_field = array("id", "hits", "modified"smilie;
$this->standard_type = array("int auto_increment primary key", "int unsigned default 0", "datetime"smilie;
}

/** creates a MySQL table if it does not yet exists. The field names were already given by construction. The corresponding types should be given here, in the same order.
* @param $type: array that contains the types of teh different field, as MySQL names then. Example: int unsigned default 0.
*
* @returns true
*/
function install($type) {
if ($this->exists())
die("This table already exists.<br />\n"smilie;

if (count($this->field) != count($type))
die("Incorrect number of fields.<br />\n"smilie;


$query = "CREATE TABLE " . $this->table . " (";
for ($i = 0; $i < count($this->standard_field); $i++) {
$query .= "" . $this->standard_field[$i] . " " . $this->standard_type[$i] . ", ";
}
for ($i = 0; $i < count($this->field); $i++) {
$query .= "" . $this->field[$i] . " " . $type[$i] . ", ";
}
$query = substr($query, 0, strlen($query)-2) . "smilie";
echo $query;
mysql_query($query);
$this->q++;
return true;
}

/**
* Checks whether the table which you are trying to use, already exists.
* @returns true if the table exists
*/
function exists() {
$query = "SHOW TABLES";
$result = mysql_query($query);
$o = false;
$n = "";
while(!$o && list($n) = mysql_fetch_row($result)) {
$o = $this->table == $n;
}
return $o;
}


/**
* This function adds a news item to the database.
* @param $element: associative array of the key/value pairs to be inserted
*
* @returns the id that is associated with the item.
*/
function add($element) {
$keys = "(modified, "; $values = "(NOW(), ";
foreach ($element as $key => $value) {
$keys .= $key . ", "; $values .= "'" . $value . "', ";
}
$keys = substr($keys, 0, strlen($keys)-2) . "smilie ";
$values = substr($values, 0, strlen($values)-2) . "smilie ";

$query = "INSERT INTO " . $this->table . " " . $keys . " VALUES " . $values;
echo $query;
$this->q++;
mysql_query($query) or error(mysql_error());
$id = mysql_insert_id();

return $id;
}

/**
* This function modifies an item in the database.
* @param $id: the id of the item item to be modified.
* @param $element: associative array of the key/value pairs to be modified
*
* @returns the id that is associated with the modified news item.
*/
function modify($id, $element) {
$query = "UPDATE " . $this->table . " SET ";
foreach($element as $key => $value) {
$query .= $key . " = '" . $value . "', ";
}
$query .= "modified = NOW() WHERE id = '" . $id . "'";
$this->q++;
mysql_query($query) or mysql_error();
return $id;
}

/**
* This function deletes an item from the database.
* @param $id: the id of the news item to be deleted.
*
* @returns true if the news item was deleted succesfully.
*/
function delete($id) {
$query = "DELETE FROM " . $this->table . " WHERE id = '$id'";
$this->q++;
mysql_query($query) or mysql_error();
return true;
}

/**
* This function gets an item from the database.
* @param $id: the id of the item you want to get from the database.
* @param $what: the field you'd like to get from the table.
*
* @returns an array that contains the data as an associative array.
*/
function get($id, $what="*"smilie {
$query = "SELECT " . $what . " FROM " . $this->table . " WHERE id = '$id' LIMIT 1";
$this->q++;
$result = mysql_query($query);
$item = mysql_fetch_assoc($result);
mysql_free_result($result);
return $item;
}

/**
* This function gets all the news headlines from the database and returns them one by one until false is returned.
* @param $limit: the number of news item to get form the database.
* @param $from: the number of the first element of the list.
* @param $what: the field you'd like to get from the table.
*
* @returns an array that contains the data as an associative array.
*/
function listing($limit=0, $from=0, $what="*"smilie {
static $result;

if (!is_resource($result)) {
$query = "SELECT " . $what . " FROM " . $this->table . " ORDER BY id DESC";
if ($limit != 0) $query .= " LIMIT $limit";
if ($from != 0) $query .= ", $from";

$this->q++;
$result = mysql_query($query);
}

$item = mysql_fetch_assoc($result);

if ($item == false) {
mysql_free_result($result);
return false;
} else
return $item;
}

/**
* This function increases the number of hits for a certain element by one.
* @param $id: the id of the element.
*
* @returns true
*/
function hit($id) {
$query = "UPDATE " . $this->table . " SET hits = hits + 1 WHERE id = '$id'";
$this->q++;
mysql_query($query) or error(mysql_error());
return true;
}
}

?>

This post was edited by Fox at 28 Aug 2007


Refuse to do what you don't want to do.
[user image]
 1 
© 2000 - 2020 BastardDomain.com