# HG changeset patch # User Heiko Schlittermann (IT Trainingshaus) # Date 1319637681 -7200 # Node ID 2a78703d3c2df328b9cb86a344f66d67e258aeb7 # Parent 6216181a76681c3453c2b4fa18f8677dbbfd8459 Added Address_Book_DB class diff -r 6216181a7668 -r 2a78703d3c2d .hgignore --- a/.hgignore Wed Oct 26 16:00:14 2011 +0200 +++ b/.hgignore Wed Oct 26 16:01:21 2011 +0200 @@ -3,3 +3,8 @@ var/abook.txt .index.php.swp var/abook.txt~ +class.Address_Book_DB.php~ +class.Address_Book_File.php~ +templates/results.html~ +test.php~ +var/abook.sqlite diff -r 6216181a7668 -r 2a78703d3c2d class.Address_Book_DB.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/class.Address_Book_DB.php Wed Oct 26 16:01:21 2011 +0200 @@ -0,0 +1,101 @@ +dbh = new PDO($dsn, $user, $pass, + array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); + + $this->dbh->exec("CREATE TABLE IF NOT EXISTS data + (name TEXT, tel TEXT, email TEXT)"); + + $this->dbh->beginTransaction(); + + $sth = $this->dbh->prepare("SELECT COUNT(*) AS COUNT FROM data"); + $sth->execute(); + $r = $sth->fetch(); + + print_r($r); + + if ($r['COUNT'] == 0) + $this->insert(array("Hans Hanson", "0815", "hans@hanson.de")); + $this->dbh->commit(); + } + + private function insert($entry) { + static $sth = null; + + if ($sth === null) + $sth = $this->dbh->prepare(self::INSERT_ENTRY); + + $sth->execute(array("name" => $entry[0], + "tel" => $entry[1], + "email" => $entry[2])); + } + + public function get_all_entries() { + $entries = array(); + fseek($this->fh, 0, SEEK_SET); + while($line = stream_get_line($this->fh, 0, self::RS)) { + if ($line === FALSE) break; + $entries[] = explode(self::FS, $line); + } + return $entries; + } + + public function add_entry($entry) { + $fields = array('name', 'tel', 'mail'); + $new = array(); + foreach($fields as $key) { + if (!isset($entry[$key])) return; + $new[$key] = $entry[$key]; + trim($new[$key]); + preg_replace('/['.self::RS.self::FS.' ]+/', ' ', $new[$key]); + if (empty($new[$key])) return; + } + + flock($this->fh, LOCK_EX); + fputs($this->fh, join(self::FS, $new) . self::RS); + flock($this->fh, LOCK_UN); + header("Location: $_SERVER[PHP_SELF]?action=add"); + exit; + } + + public function search_entries($pattern) { + static $sth = null; + + if ($sth === null) + $sth = $this->dbh->prepare(self::SELECT_ENTRY); + + // $pattern = "%" . trim(trim($pattern), "%") . "%"; + + $pattern = trim($pattern); + $pattern = trim($pattern, '%'); + $pattern = "%$pattern%"; + + $sth->execute(array("name" => $pattern, + "tel" => $pattern, + "email" => $pattern)); + $entries = array(); + while ($r = $sth->fetch()) { + $entries[] = array('name' => $r['NAME'], + 'tel' => $r['TEL'], + 'mail' => $r['EMAIL']); + } + return $entries; + } + + public function delete_entries($ids) { } +} + +?> diff -r 6216181a7668 -r 2a78703d3c2d index.php --- a/index.php Wed Oct 26 16:00:14 2011 +0200 +++ b/index.php Wed Oct 26 16:01:21 2011 +0200 @@ -15,7 +15,8 @@ } try { - $abook = new Address_Book_File(FILE); + //$abook = new Address_Book_DB("mysql:host=localhost;dbname=abook", "hans", "x"); + $abook = new Address_Book_DB("sqlite:var/abook.sqlite"); switch (@$_REQUEST['action']) { case 'add': $abook->add_entry($_REQUEST); break;