cvs: presentations /slides/mvc/example add_c.inc add_c.phps /slides/mvc/example/model db.inc db.phps items.inc items.phps
| From: | Rasmus Lerdorf | Date: | Thu, 02 Mar 2006 04:11:06 +0000 |
| Subject: | cvs: presentations /slides/mvc/example add_c.inc add_c.phps /slides/mvc/example/model db.inc db.phps items.inc items.phps | ||
| Groups: | php.pres | ||
| Request: | Send a blank email to pres+get-1266@lists.php.net to get a copy of this message | ||
rasmus Thu Mar 2 04:11:06 2006 UTC
Modified files:
/presentations/slides/mvc/example add_c.inc add_c.phps
/presentations/slides/mvc/example/model db.inc db.phps items.inc
items.phps
Log:
Stick the OOP data model layer back in by popular demand
http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/add_c.inc?r1=1.2&r2=1.3&diff_format=u Index: presentations/slides/mvc/example/add_c.inc diff -u presentations/slides/mvc/example/add_c.inc:1.2 presentations/slides/mvc/example/add_c.inc:1.3 --- presentations/slides/mvc/example/add_c.inc:1.2 Tue Feb 21 21:26:21 2006 +++ presentations/slides/mvc/example/add_c.inc Thu Mar 2 04:11:06 2006 @@ -1,11 +1,12 @@ <?php include './model/db.inc'; include './model/items.inc'; +$db = new items(); if($_SERVER['REQUEST_METHOD']=='POST') { header("Content-type: application/json"); // Load an item entry from backend and send JSON request to populate form if(isset($_POST['load_item'])) { - $entry = load_items($_POST['load_item']); + $entry = $db->load($_POST['load_item']); $entry[0]['submit'] = 'Modify Item'; if($entry) echo json_encode(array('formName'=>$_POST['formName'], 'load_item'=>$entry)); @@ -23,10 +24,10 @@ // Save changes and display status message $status = "Failure"; if($_POST['f_submit']=='Modify Item') { - $ret = modify_item($_POST); + $ret = $db->modify($_POST); if($ret) $status = "Modified"; } else { - $ret = new_item($_POST); + $ret = $db->insert($_POST); if($ret) $status = "Added"; } echo json_encode(array('status'=>$status, @@ -39,5 +40,5 @@ // Initialize view data if(!isset($categories)) load_list('categories'); if(!isset($item)) $item = array('cat'=>''); -$items = load_items(); +$items = $db->load(); ?> http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/add_c.phps?r1=1.1&r2=1.2&diff_format=u Index: presentations/slides/mvc/example/add_c.phps diff -u presentations/slides/mvc/example/add_c.phps:1.1 presentations/slides/mvc/example/add_c.phps:1.2 --- presentations/slides/mvc/example/add_c.phps:1.1 Mon Feb 27 19:08:50 2006 +++ presentations/slides/mvc/example/add_c.phps Thu Mar 2 04:11:06 2006 @@ -1,11 +1,12 @@ <?php include './model/db.inc'; include './model/items.inc'; +$db = new items(); if($_SERVER['REQUEST_METHOD']=='POST') { header("Content-type: application/json"); // Load an item entry from backend and send JSON request to populate form if(isset($_POST['load_item'])) { - $entry = load_items($_POST['load_item']); + $entry = $db->load($_POST['load_item']); $entry[0]['submit'] = 'Modify Item'; if($entry) echo json_encode(array('formName'=>$_POST['formName'], 'load_item'=>$entry)); @@ -23,10 +24,10 @@ // Save changes and display status message $status = "Failure"; if($_POST['f_submit']=='Modify Item') { - $ret = modify_item($_POST); + $ret = $db->modify($_POST); if($ret) $status = "Modified"; } else { - $ret = new_item($_POST); + $ret = $db->insert($_POST); if($ret) $status = "Added"; } echo json_encode(array('status'=>$status, @@ -39,5 +40,5 @@ // Initialize view data if(!isset($categories)) load_list('categories'); if(!isset($item)) $item = array('cat'=>''); -$items = load_items(); +$items = $db->load(); ?> http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/model/db.inc?r1=1.1&r2=1.2&diff_format=u Index: presentations/slides/mvc/example/model/db.inc diff -u presentations/slides/mvc/example/model/db.inc:1.1 presentations/slides/mvc/example/model/db.inc:1.2 --- presentations/slides/mvc/example/model/db.inc:1.1 Wed Jan 25 02:32:01 2006 +++ presentations/slides/mvc/example/model/db.inc Thu Mar 2 04:11:06 2006 @@ -1,20 +1,23 @@ <?php -function db_connect() { - $dbh = new PDO('sqlite:./model/example.db'); - $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - return $dbh; -} +class db { + public $dbh = false; + + function connect() { + $this->dbh = new PDO('sqlite:./model/example.db'); + $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } -function db_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"; + 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(); } - echo "</pre>"; - die(); } // load_list takes a text file and turns it into a global array cached by APC http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/model/db.phps?r1=1.1&r2=1.2&diff_format=u Index: presentations/slides/mvc/example/model/db.phps diff -u presentations/slides/mvc/example/model/db.phps:1.1 presentations/slides/mvc/example/model/db.phps:1.2 --- presentations/slides/mvc/example/model/db.phps:1.1 Mon Feb 27 19:08:50 2006 +++ presentations/slides/mvc/example/model/db.phps Thu Mar 2 04:11:06 2006 @@ -1,20 +1,23 @@ <?php -function db_connect() { - $dbh = new PDO('sqlite:./model/example.db'); - $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - return $dbh; -} +class db { + public $dbh = false; + + function connect() { + $this->dbh = new PDO('sqlite:./model/example.db'); + $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } -function db_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"; + 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(); } - echo "</pre>"; - die(); } // load_list takes a text file and turns it into a global array cached by APC http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/model/items.inc?r1=1.3&r2=1.4&diff_format=u Index: presentations/slides/mvc/example/model/items.inc diff -u presentations/slides/mvc/example/model/items.inc:1.3 presentations/slides/mvc/example/model/items.inc:1.4 --- presentations/slides/mvc/example/model/items.inc:1.3 Wed Mar 1 19:23:17 2006 +++ presentations/slides/mvc/example/model/items.inc Thu Mar 2 04:11:06 2006 @@ -7,66 +7,63 @@ price REAL DEFAULT 0 */ -function new_item($item) { - global $dbh; - $t = $_SERVER["REQUEST_TIME"]; - try { - if(empty($dbh)) $dbh = db_connect(); - $stmt = $dbh->prepare("INSERT INTO items - (id,cat,sdesc,ldesc,price,ctime) - VALUES - (NULL,:cat,:sdesc,:ldesc,:price,$t)"); - $stmt->bindParam(':cat', $item['cat']); - $stmt->bindParam(':sdesc', $item['sdesc']); - $stmt->bindParam(':ldesc', $item['ldesc']); - $stmt->bindParam(':price', $item['price']); - $ret = $stmt->execute(); - } catch (PDOException $e) { - db_fatal_error($e->getMessage()); +class items extends db { + function insert($item) { + $t = $_SERVER["REQUEST_TIME"]; + try { + if(!$this->dbh) $this->connect(); + $stmt = $this->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); + } catch (PDOException $e) { + $this->fatal_error($e->getMessage()); + } + return $ret; } - return $ret; -} - -function modify_item($item) { - global $dbh; - try { - if(empty($dbh)) $dbh = db_connect(); - $stmt = $dbh->prepare("UPDATE items SET - cat=:cat, sdesc=:sdesc, - ldesc=:ldesc, price=:price - WHERE id=:id"); - $stmt->bindParam(':cat', $item['cat']); - $stmt->bindParam(':sdesc', $item['sdesc']); - $stmt->bindParam(':ldesc', $item['ldesc']); - $stmt->bindParam(':price', $item['price']); - $stmt->bindParam(':id', $item['id']); - $ret = $stmt->execute(); - } catch (PDOException $e) { - db_fatal_error($e->getMessage()); + function modify($item) { + try { + if(!$this->dbh) $this->connect(); + $stmt = $this->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); + } catch (PDOException $e) { + $this->fatal_error($e->getMessage()); + } + return $ret; } - return $ret; -} -function load_items($id=-1) { - global $dbh; - $where = ''; + function load($id=-1) { + $where = ''; - if($id!=-1) $where = "where id=".(int)$id; - try { - if(empty($dbh)) $dbh = db_connect(); - $result = $dbh->query("SELECT * FROM items - $where order by ctime desc"); - $rows = $result->fetchAll(PDO::FETCH_ASSOC); - } catch (PDOException $e) { - db_fatal_error($e->getMessage()); + if($id!=-1) $where = "where id=".(int)$id; + try { + if(!$this->dbh) $this->connect(); + $result = $this->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; } - // 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; } - ?> http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/model/items.phps?r1=1.2&r2=1.3&diff_format=u Index: presentations/slides/mvc/example/model/items.phps diff -u presentations/slides/mvc/example/model/items.phps:1.2 presentations/slides/mvc/example/model/items.phps:1.3 --- presentations/slides/mvc/example/model/items.phps:1.2 Wed Mar 1 19:23:17 2006 +++ presentations/slides/mvc/example/model/items.phps Thu Mar 2 04:11:06 2006 @@ -7,66 +7,63 @@ price REAL DEFAULT 0 */ -function new_item($item) { - global $dbh; - $t = $_SERVER["REQUEST_TIME"]; - try { - if(empty($dbh)) $dbh = db_connect(); - $stmt = $dbh->prepare("INSERT INTO items - (id,cat,sdesc,ldesc,price,ctime) - VALUES - (NULL,:cat,:sdesc,:ldesc,:price,$t)"); - $stmt->bindParam(':cat', $item['cat']); - $stmt->bindParam(':sdesc', $item['sdesc']); - $stmt->bindParam(':ldesc', $item['ldesc']); - $stmt->bindParam(':price', $item['price']); - $ret = $stmt->execute(); - } catch (PDOException $e) { - db_fatal_error($e->getMessage()); +class items extends db { + function insert($item) { + $t = $_SERVER["REQUEST_TIME"]; + try { + if(!$this->dbh) $this->connect(); + $stmt = $this->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); + } catch (PDOException $e) { + $this->fatal_error($e->getMessage()); + } + return $ret; } - return $ret; -} - -function modify_item($item) { - global $dbh; - try { - if(empty($dbh)) $dbh = db_connect(); - $stmt = $dbh->prepare("UPDATE items SET - cat=:cat, sdesc=:sdesc, - ldesc=:ldesc, price=:price - WHERE id=:id"); - $stmt->bindParam(':cat', $item['cat']); - $stmt->bindParam(':sdesc', $item['sdesc']); - $stmt->bindParam(':ldesc', $item['ldesc']); - $stmt->bindParam(':price', $item['price']); - $stmt->bindParam(':id', $item['id']); - $ret = $stmt->execute(); - } catch (PDOException $e) { - db_fatal_error($e->getMessage()); + function modify($item) { + try { + if(!$this->dbh) $this->connect(); + $stmt = $this->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); + } catch (PDOException $e) { + $this->fatal_error($e->getMessage()); + } + return $ret; } - return $ret; -} -function load_items($id=-1) { - global $dbh; - $where = ''; + function load($id=-1) { + $where = ''; - if($id!=-1) $where = "where id=".(int)$id; - try { - if(empty($dbh)) $dbh = db_connect(); - $result = $dbh->query("SELECT * FROM items - $where order by ctime desc"); - $rows = $result->fetchAll(PDO::FETCH_ASSOC); - } catch (PDOException $e) { - db_fatal_error($e->getMessage()); + if($id!=-1) $where = "where id=".(int)$id; + try { + if(!$this->dbh) $this->connect(); + $result = $this->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; } - // 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; } - ?>
http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/add_c.inc?r1=1.2&r2=1.3&diff_format=u Index: presentations/slides/mvc/example/add_c.inc diff -u presentations/slides/mvc/example/add_c.inc:1.2 presentations/slides/mvc/example/add_c.inc:1.3 --- presentations/slides/mvc/example/add_c.inc:1.2 Tue Feb 21 21:26:21 2006 +++ presentations/slides/mvc/example/add_c.inc Thu Mar 2 04:11:06 2006 @@ -1,11 +1,12 @@ <?php include './model/db.inc'; include './model/items.inc'; +$db = new items(); if($_SERVER['REQUEST_METHOD']=='POST') { header("Content-type: application/json"); // Load an item entry from backend and send JSON request to populate form if(isset($_POST['load_item'])) { - $entry = load_items($_POST['load_item']); + $entry = $db->load($_POST['load_item']); $entry[0]['submit'] = 'Modify Item'; if($entry) echo json_encode(array('formName'=>$_POST['formName'], 'load_item'=>$entry)); @@ -23,10 +24,10 @@ // Save changes and display status message $status = "Failure"; if($_POST['f_submit']=='Modify Item') { - $ret = modify_item($_POST); + $ret = $db->modify($_POST); if($ret) $status = "Modified"; } else { - $ret = new_item($_POST); + $ret = $db->insert($_POST); if($ret) $status = "Added"; } echo json_encode(array('status'=>$status, @@ -39,5 +40,5 @@ // Initialize view data if(!isset($categories)) load_list('categories'); if(!isset($item)) $item = array('cat'=>''); -$items = load_items(); +$items = $db->load(); ?> http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/add_c.phps?r1=1.1&r2=1.2&diff_format=u Index: presentations/slides/mvc/example/add_c.phps diff -u presentations/slides/mvc/example/add_c.phps:1.1 presentations/slides/mvc/example/add_c.phps:1.2 --- presentations/slides/mvc/example/add_c.phps:1.1 Mon Feb 27 19:08:50 2006 +++ presentations/slides/mvc/example/add_c.phps Thu Mar 2 04:11:06 2006 @@ -1,11 +1,12 @@ <?php include './model/db.inc'; include './model/items.inc'; +$db = new items(); if($_SERVER['REQUEST_METHOD']=='POST') { header("Content-type: application/json"); // Load an item entry from backend and send JSON request to populate form if(isset($_POST['load_item'])) { - $entry = load_items($_POST['load_item']); + $entry = $db->load($_POST['load_item']); $entry[0]['submit'] = 'Modify Item'; if($entry) echo json_encode(array('formName'=>$_POST['formName'], 'load_item'=>$entry)); @@ -23,10 +24,10 @@ // Save changes and display status message $status = "Failure"; if($_POST['f_submit']=='Modify Item') { - $ret = modify_item($_POST); + $ret = $db->modify($_POST); if($ret) $status = "Modified"; } else { - $ret = new_item($_POST); + $ret = $db->insert($_POST); if($ret) $status = "Added"; } echo json_encode(array('status'=>$status, @@ -39,5 +40,5 @@ // Initialize view data if(!isset($categories)) load_list('categories'); if(!isset($item)) $item = array('cat'=>''); -$items = load_items(); +$items = $db->load(); ?> http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/model/db.inc?r1=1.1&r2=1.2&diff_format=u Index: presentations/slides/mvc/example/model/db.inc diff -u presentations/slides/mvc/example/model/db.inc:1.1 presentations/slides/mvc/example/model/db.inc:1.2 --- presentations/slides/mvc/example/model/db.inc:1.1 Wed Jan 25 02:32:01 2006 +++ presentations/slides/mvc/example/model/db.inc Thu Mar 2 04:11:06 2006 @@ -1,20 +1,23 @@ <?php -function db_connect() { - $dbh = new PDO('sqlite:./model/example.db'); - $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - return $dbh; -} +class db { + public $dbh = false; + + function connect() { + $this->dbh = new PDO('sqlite:./model/example.db'); + $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } -function db_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"; + 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(); } - echo "</pre>"; - die(); } // load_list takes a text file and turns it into a global array cached by APC http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/model/db.phps?r1=1.1&r2=1.2&diff_format=u Index: presentations/slides/mvc/example/model/db.phps diff -u presentations/slides/mvc/example/model/db.phps:1.1 presentations/slides/mvc/example/model/db.phps:1.2 --- presentations/slides/mvc/example/model/db.phps:1.1 Mon Feb 27 19:08:50 2006 +++ presentations/slides/mvc/example/model/db.phps Thu Mar 2 04:11:06 2006 @@ -1,20 +1,23 @@ <?php -function db_connect() { - $dbh = new PDO('sqlite:./model/example.db'); - $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - return $dbh; -} +class db { + public $dbh = false; + + function connect() { + $this->dbh = new PDO('sqlite:./model/example.db'); + $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } -function db_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"; + 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(); } - echo "</pre>"; - die(); } // load_list takes a text file and turns it into a global array cached by APC http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/model/items.inc?r1=1.3&r2=1.4&diff_format=u Index: presentations/slides/mvc/example/model/items.inc diff -u presentations/slides/mvc/example/model/items.inc:1.3 presentations/slides/mvc/example/model/items.inc:1.4 --- presentations/slides/mvc/example/model/items.inc:1.3 Wed Mar 1 19:23:17 2006 +++ presentations/slides/mvc/example/model/items.inc Thu Mar 2 04:11:06 2006 @@ -7,66 +7,63 @@ price REAL DEFAULT 0 */ -function new_item($item) { - global $dbh; - $t = $_SERVER["REQUEST_TIME"]; - try { - if(empty($dbh)) $dbh = db_connect(); - $stmt = $dbh->prepare("INSERT INTO items - (id,cat,sdesc,ldesc,price,ctime) - VALUES - (NULL,:cat,:sdesc,:ldesc,:price,$t)"); - $stmt->bindParam(':cat', $item['cat']); - $stmt->bindParam(':sdesc', $item['sdesc']); - $stmt->bindParam(':ldesc', $item['ldesc']); - $stmt->bindParam(':price', $item['price']); - $ret = $stmt->execute(); - } catch (PDOException $e) { - db_fatal_error($e->getMessage()); +class items extends db { + function insert($item) { + $t = $_SERVER["REQUEST_TIME"]; + try { + if(!$this->dbh) $this->connect(); + $stmt = $this->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); + } catch (PDOException $e) { + $this->fatal_error($e->getMessage()); + } + return $ret; } - return $ret; -} - -function modify_item($item) { - global $dbh; - try { - if(empty($dbh)) $dbh = db_connect(); - $stmt = $dbh->prepare("UPDATE items SET - cat=:cat, sdesc=:sdesc, - ldesc=:ldesc, price=:price - WHERE id=:id"); - $stmt->bindParam(':cat', $item['cat']); - $stmt->bindParam(':sdesc', $item['sdesc']); - $stmt->bindParam(':ldesc', $item['ldesc']); - $stmt->bindParam(':price', $item['price']); - $stmt->bindParam(':id', $item['id']); - $ret = $stmt->execute(); - } catch (PDOException $e) { - db_fatal_error($e->getMessage()); + function modify($item) { + try { + if(!$this->dbh) $this->connect(); + $stmt = $this->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); + } catch (PDOException $e) { + $this->fatal_error($e->getMessage()); + } + return $ret; } - return $ret; -} -function load_items($id=-1) { - global $dbh; - $where = ''; + function load($id=-1) { + $where = ''; - if($id!=-1) $where = "where id=".(int)$id; - try { - if(empty($dbh)) $dbh = db_connect(); - $result = $dbh->query("SELECT * FROM items - $where order by ctime desc"); - $rows = $result->fetchAll(PDO::FETCH_ASSOC); - } catch (PDOException $e) { - db_fatal_error($e->getMessage()); + if($id!=-1) $where = "where id=".(int)$id; + try { + if(!$this->dbh) $this->connect(); + $result = $this->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; } - // 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; } - ?> http://cvs.php.net/viewcvs.cgi/presentations/slides/mvc/example/model/items.phps?r1=1.2&r2=1.3&diff_format=u Index: presentations/slides/mvc/example/model/items.phps diff -u presentations/slides/mvc/example/model/items.phps:1.2 presentations/slides/mvc/example/model/items.phps:1.3 --- presentations/slides/mvc/example/model/items.phps:1.2 Wed Mar 1 19:23:17 2006 +++ presentations/slides/mvc/example/model/items.phps Thu Mar 2 04:11:06 2006 @@ -7,66 +7,63 @@ price REAL DEFAULT 0 */ -function new_item($item) { - global $dbh; - $t = $_SERVER["REQUEST_TIME"]; - try { - if(empty($dbh)) $dbh = db_connect(); - $stmt = $dbh->prepare("INSERT INTO items - (id,cat,sdesc,ldesc,price,ctime) - VALUES - (NULL,:cat,:sdesc,:ldesc,:price,$t)"); - $stmt->bindParam(':cat', $item['cat']); - $stmt->bindParam(':sdesc', $item['sdesc']); - $stmt->bindParam(':ldesc', $item['ldesc']); - $stmt->bindParam(':price', $item['price']); - $ret = $stmt->execute(); - } catch (PDOException $e) { - db_fatal_error($e->getMessage()); +class items extends db { + function insert($item) { + $t = $_SERVER["REQUEST_TIME"]; + try { + if(!$this->dbh) $this->connect(); + $stmt = $this->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); + } catch (PDOException $e) { + $this->fatal_error($e->getMessage()); + } + return $ret; } - return $ret; -} - -function modify_item($item) { - global $dbh; - try { - if(empty($dbh)) $dbh = db_connect(); - $stmt = $dbh->prepare("UPDATE items SET - cat=:cat, sdesc=:sdesc, - ldesc=:ldesc, price=:price - WHERE id=:id"); - $stmt->bindParam(':cat', $item['cat']); - $stmt->bindParam(':sdesc', $item['sdesc']); - $stmt->bindParam(':ldesc', $item['ldesc']); - $stmt->bindParam(':price', $item['price']); - $stmt->bindParam(':id', $item['id']); - $ret = $stmt->execute(); - } catch (PDOException $e) { - db_fatal_error($e->getMessage()); + function modify($item) { + try { + if(!$this->dbh) $this->connect(); + $stmt = $this->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); + } catch (PDOException $e) { + $this->fatal_error($e->getMessage()); + } + return $ret; } - return $ret; -} -function load_items($id=-1) { - global $dbh; - $where = ''; + function load($id=-1) { + $where = ''; - if($id!=-1) $where = "where id=".(int)$id; - try { - if(empty($dbh)) $dbh = db_connect(); - $result = $dbh->query("SELECT * FROM items - $where order by ctime desc"); - $rows = $result->fetchAll(PDO::FETCH_ASSOC); - } catch (PDOException $e) { - db_fatal_error($e->getMessage()); + if($id!=-1) $where = "where id=".(int)$id; + try { + if(!$this->dbh) $this->connect(); + $result = $this->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; } - // 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; } - ?>