<?php
class db {
protected static $dbh = false;
function connect() {
self::$dbh = new PDO('sqlite:./model/example.db');
self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
protected function fatal_error($msg) {
echo "<pre>Error!: $msg\n";
$bt = debug_backtrace();
foreach($bt as $line) {
$args = var_export($line['args'], true);
echo "{$line['function']}($args) at {$line['file']}:{$line['line']}\n";
}
echo "</pre>";
die();
}
}
// load_list takes a text file and turns it into a global array cached by APC
function load_list($name) {
global $$name;
if(!$$name = apc_fetch($name)) {
$$name = explode("\n",trim(file_get_contents($name.'.txt')));
apc_store($name,$$name);
}
}
?>
<?php
/*
id INTEGER PRIMARY KEY,
cat TEXT NOT NULL,
sdesc TEXT NOT NULL,
ldesc TEXT NOT NULL,
price REAL DEFAULT 0
*/
class items extends db {
function insert($item) {
$t = $_SERVER["REQUEST_TIME"];
try {
if(!self::$dbh) $this->connect();
self::$dbh->beginTransaction();
$stmt = self::$dbh->prepare("INSERT INTO items
(id,cat,sdesc,ldesc,price,ctime)
VALUES
(NULL,:cat,:sdesc,:ldesc,:price,$t)");
$params = array(':cat' =>$item['cat'],
':sdesc'=>$item['sdesc'],
':ldesc'=>$item['ldesc'],
':price'=>$item['price']);
$ret = $stmt->execute($params);
self::$dbh->commit();
} catch (PDOException $e) {
self::$dbh->rollback();
$this->fatal_error($e->getMessage());
}
return $ret;
}
function modify($item) {
try {
if(!self::$dbh) $this->connect();
self::$dbh->beginTransaction();
$stmt = self::$dbh->prepare("UPDATE items SET
cat=:cat, sdesc=:sdesc,
ldesc=:ldesc, price=:price
WHERE id=:id");
$params = array(':cat' =>$item['cat'],
':sdesc'=>$item['sdesc'],
':ldesc'=>$item['ldesc'],
':price'=>$item['price'],
':id'=>$item['id']);
$ret = $stmt->execute($params);
self::$dbh->commit();
} catch (PDOException $e) {
self::$dbh->rollback();
$this->fatal_error($e->getMessage());
}
return $ret;
}
function load($id=-1) {
$where = '';
if($id!=-1) $where = "where id=".(int)$id;
try {
if(!self::$dbh) $this->connect();
$result = self::$dbh->query("SELECT * FROM items
$where order by ctime desc");
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$this->fatal_error($e->getMessage());
}
// Some databases can do this right in the SELECT
// SQLite can't, so we add a formatted column ourselves
foreach($rows as $i=>$row) {
$rows[$i]['fprice'] = number_format($row['price'],2);
}
return $rows;
}
}
?>