Home  |  Linux  | Mysql  | PHP  | XML
From:Rasmus Lerdorf Date:Sun Jul 20 00:19:21 2008
Subject:cvs: presentations /slides/intro twit_db1.xml twit_db2.xml twit_db3.xml twit_db4.xml twit_db5.xml twit_db6.xml
rasmus		Sun Jul 20 06:19:21 2008 UTC

  Added files:                 
    /presentations/slides/intro	twit_db1.xml twit_db2.xml twit_db3.xml 
                               	twit_db4.xml twit_db5.xml twit_db6.xml 
  Log:
  .
  
  

http://cvs.php.net/viewvc.cgi/presentations/slides/intro/twit_db1.xml?view=markup&rev=1.1
Index: presentations/slides/intro/twit_db1.xml
+++ presentations/slides/intro/twit_db1.xml
<slide title="DB Layer">

<break lines="1" />
<blurb fontsize="7em">
A simple abstract base class is the start of our Database layer
</blurb>

<example result="0" marginright="1em" fontsize="1.5"><![CDATA[<?php
abstract class db {
  protected static $dbh = false;

  function connect() {
    self::$dbh = new PDO('sqlite:./people.db');
    self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  }
?>]]></example>

</slide>

http://cvs.php.net/viewvc.cgi/presentations/slides/intro/twit_db2.xml?view=markup&rev=1.1
Index: presentations/slides/intro/twit_db2.xml
+++ presentations/slides/intro/twit_db2.xml
<slide title="DB Layer">

<break lines="1" />
<blurb fontsize="7em">
This will help us track down problems during development
</blurb>

<example result="0" marginright="1em" fontsize="1.5"><![CDATA[<?php
abstract class db {
  protected static $dbh = false;

  function connect() {
    self::$dbh = new PDO('sqlite:./people.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();
  }
?>]]></example>

</slide>

http://cvs.php.net/viewvc.cgi/presentations/slides/intro/twit_db3.xml?view=markup&rev=1.1
Index: presentations/slides/intro/twit_db3.xml
+++ presentations/slides/intro/twit_db3.xml
<slide title="DB Layer">

<break lines="1" />
<blurb fontsize="7em">
For each table I usually do something like this
</blurb>

<example result="0" marginright="1em" fontsize="1.5"><![CDATA[<?php
class twits extends db {
  function load($id="",$lx=-1,$ly=-1) {
    if(!self::$dbh) $this->connect();
    if(strlen($id)) {
      $id = self::$dbh->quote($id);
      $where = "WHERE id=$id";
    } else $where = "status > -1";
    if($lx!=-1) {
      $limit = "LIMIT $lx,$ly";
    } else $limit="";
    try {
      $result = self::$dbh->query("SELECT * from twits $where order by ctime desc $limit");
      $rows = $result->fetchall(PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
      $this->fatal_error($e->getMessage());
    }
    if(strlen($id) && count($rows)) return $rows[0];
    return $rows;
  }
}
?>]]></example>

</slide>

http://cvs.php.net/viewvc.cgi/presentations/slides/intro/twit_db4.xml?view=markup&rev=1.1
Index: presentations/slides/intro/twit_db4.xml
+++ presentations/slides/intro/twit_db4.xml
<slide title="DB Layer">

<break lines="1" />
<blurb fontsize="7em">
Prepare/execute example
</blurb>

<example result="0" marginright="1em" fontsize="1.5"><![CDATA[<?php
class people extends db {
  function load($id="",$lx=-1,$ly=-1) {
    if(!self::$dbh) $this->connect();
    if(strlen($id)) {
      $id = self::$dbh->quote($id);
      $where = "WHERE id=$id";
    } else $where = "status > -1";
    if($lx!=-1) {
      $limit = "LIMIT $lx,$ly";
    } else $limit="";
    try {
      $result = self::$dbh->query("SELECT * from twits $where order by ctime desc $limit");
      $rows = $result->fetchall(PDO::FETCH_ASSOC);
    } catch (PDOException $e) {
      $this->fatal_error($e->getMessage());
    }
    if(strlen($id) && count($rows)) return $rows[0];
    return $rows;
  }

  function save($record) {
    if(!strlen($record['id'])) return false;
    if(!self::$dbh) $this->connect();
    try {
      $stmt = self::$dbh->prepare("INSERT IGNORE INTO twits 
         (id,ctime,msg,to_user_id,from_user,from_user_id,iso,profile_image,query,assigned) 
         VALUES (:id,FROM_UNIXTIME(:ctime),:msg,:to_user_id,:from_user,:from_user_id,:iso,
                 :profile_image,:query,:assigned)");
      $params = array(':id'=>$record['id'], ':ctime'=>strtotime($record['created_at']),
                      ':msg'=>$record['text'], ':to_user_id'=>$record['to_user_id'],
                      ':from_user'=>$record['from_user'],':from_user_id'=>$record['from_user_id'],
                      ':iso'=>$record['iso_language_code'],':profile_image'=>$record['profile_image_url'],
                      ':query'=>$record['query'],':assigned'=>$record['assigned']);
      $stmt->execute($params);
    } catch (PDOException $e) {
      $this->fatal_error($e->getMessage());
    }
  }

?>]]></example>

</slide>

http://cvs.php.net/viewvc.cgi/presentations/slides/intro/twit_db5.xml?view=markup&rev=1.1
Index: presentations/slides/intro/twit_db5.xml
+++ presentations/slides/intro/twit_db5.xml
<slide title="DB Layer">

<break lines="1" />
<blurb fontsize="7em">
Using the DB layer
</blurb>

<example result="0" marginright="1em"><![CDATA[<?php
  include './db.inc';
  include './db_twits.inc';

  $twits = new twits; 
  $twits->save($_POST);
?>]]></example>

</slide>

http://cvs.php.net/viewvc.cgi/presentations/slides/intro/twit_db6.xml?view=markup&rev=1.1
Index: presentations/slides/intro/twit_db6.xml
+++ presentations/slides/intro/twit_db6.xml
<slide title="DB Layer">

<break lines="1" />
<blurb fontsize="7em">
A complete DB/JSON backend
</blurb>

<example result="0" marginright="1em"><![CDATA[<?php
  include './db.inc';
  include './db_twits.inc';

  $twits = new twits;
  $query = $_POST['query'];

  header("Content-type: application/json");

  switch($_REQUEST['action']) {
    case 'remove_old':
       $twits->remove_old($query,$_SERVER['REQUEST_TIME']-(14*24*3600));
       echo json_encode('update'=>'Old tweets have been removed');
       break;
  }
?>]]></example>

</slide>


Navigate in group php.pres at sever news.php.net
Previous Next




  
© No Copyright
You are free to use Anything
Site Maintained by PHP Developer
Powered By PHP Consultants