我是正常蛇

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

PHP iterator practise

先给数据库简单建个books表,简单插入 7 8个数据:

 1 CREATE TABLE IF NOT EXISTS `books` (
 2 `id` int(11) NOT NULL AUTO_INCREMENT,
 3 `ISBN` varchar(20) DEFAULT NULL,
 4 `title` varchar(60) DEFAULT NULL,
 5 `publisher` int(11) DEFAULT NULL,
 6 PRIMARY KEY (`id`),
 7 UNIQUE KEY `ISBN` (`ISBN`)
 8 ) ENGINE=MyISAM DEFAULT CHARSET=gbk AUTO_INCREMENT=10 ;
 9 
10 INSERT INTO `books` (`id`, `ISBN`, `title`, `publisher`) VALUES
11 (1, '1111', 't11111', 1),
12 (2, '2222', 't22222', 1),
13 (3, '3333', 't33333', 1),
14 (4, '4444', 't44444', 2),
15 (5, '5555', 't55555', 2),
16 (6, '6666', 't66666', 2),
17 (7, '7777', 't77777', 3),
18 (8, '8888', 't88888', 3),
19 (9, '9999', 't99999', 3);

 

简单写了个sql类,方便数据库处理操作:

class Sql{
 
private $host;
private $user;
private $password;
private $dbname;
private $conn;
private $result;
 
public function __construct($host,$user,$password,$dbname){
     $this->host= $host;
     $this->user= $user;
     $this->password= $password;
     $this->dbname= $dbname;
     $this->conn= mysql_connect($this->host,$this->user,$this->password) or die('mysql connected failed!!!');
     mysql_select_db($this->dbname,$this->conn) or die('mysql select db failed!!!');
}
 
public function query($sql){
     $this->result= mysql_query($sql,$this->conn) or die('mysql query failed!!');
}
 
public function fetch_all(){
     $data = array();
     while($sin = mysql_fetch_array($this->result)){
          array_push($data, $sin);
     }
     return $data;
}
}

 

创建Books类,并使其有Iterator样式:

 1 class Books implements Iterator{
 2  
 3 private $dataPerPage = 3; //the number of data of a single page.
 4 private $currentPage = 1; //the current page number
 5 private $pages = array(); //all the datas of a page will be pushed into it.
 6 private $all;
 7 private $db;
 8  
 9 public function __construct(){
10      $this->db = new Sql('localhost', 'root', '123123', 'test1');
11      $this->db->query('select count(*) from `books`');
12      $data = $this->db->fetch_all();
13      $this->all = $data[0][0];
14 }
15  
16 public function current () {
17      return current($this->pages[$this->currentPage]);
18 }
19  
20 public function next () {
21      next($this->pages[$this->currentPage]);
22      if(key($this->pages[$this->currentPage]) === null && count($this->pages[$this->currentPage]) == $this->dataPerPage){
23           echo '<hr>';
24           $this->currentPage++;
25           if(is_array(@$this->pages[$this->currentPage]))
26           reset($this->pages[$this->currentPage]);
27      }
28 }
29 
30 public function key () {
31      return key($this->pages[$this->currentPage]) + ($this->currentPage-1)*$this->dataPerPage;
32 }
33  
34 public function valid () {
35      if(!array_key_exists($this->currentPage, $this->pages)){
36           $page = $this->getPage();
37           if($page == null){
38                return false;
39           }else{
40                $this->pages[$this->currentPage] = $page;
41                return true;
42           }
43      }else{
44           if(key($this->pages[$this->currentPage]) == null){
45                return false;
46           }else{
47                return true;
48           }
49      }
50 }
51 
52 public function rewind () {
53      $this->currentPage = 1;
54      foreach ($this->pages as $v){
55           reset($v);
56      }
57 }
58  
59 public function count(){
60      return $this->all;
61 }
62  
63 public function seek($index){
64      if($index < 0 || $index > $this->all)
65           throw new OutOfBoundsException();
66      $this->currentPage = (int)floor($index/$this->dataPerPage) + 1;
67      $page = $this->getPage();
68      reset($page);
69      for($i = $index % $this->dataPerPage;$i > 0;$i--){
70           next($page);
71      }
72 }
73  
74 private function getPage(){
75      $sql = 'select * from `books` limit ' . $this->dataPerPage * ($this->currentPage - 1) . ',' . $this->dataPerPage;
76      $this->db->query($sql);
77      $page = $this->db->fetch_all();
78      return $page;
79 }
80 }

 

创建扩展类:

class Test1 extends IteratorIterator{
 
private $currentItems;
private $page;
private $itemsPerPage;
 
public function __construct($iterator,$page,$itemsPerPage){
     parent::__construct($iterator);
     $this->page= $page;
     $this->itemsPerPage= $itemsPerPage;
}
 
public function rewind(){
     $currentItem = 0;
     $this->getInnerIterator()->seek($this->itemsPerPage*($this->page-1 ));
}
 
public function valid(){
     return ($this->currentItems!= $this->itemsPerPage&& $this->getInnerIterator()->valid());
}
 
public function key(){
     return $this->getInnerIterator()->key();
}
 
public function current(){
     return $this->getInnerIterator()->current();
}
 
public function next(){
     $this->getInnerIterator()->next();
     $this->currentItems++;
}
}

 

下面增加测试代码:

 1 $a = new Books();
 2 
 3  
 4 
 5 for($a->rewind();$a->valid();$a->next()){
 6 
 7 echo 'the key is :';
 8 
 9 var_dump($a->key());
10 
11 echo "<br> the value is :";
12 
13 var_dump($a->current());
14 
15 echo "<br><br>";
16 
17 }

 

页面输入如下:

the key is :int(0) 
the value is :array(8) { [0]=> string(1) "1" ["id"]=> string(1) "1" [1]=> string(4) "1111" ["ISBN"]=> string(4) "1111" [2]=> string(6) "t11111" ["title"]=> string(6) "t11111" [3]=> string(1) "1" ["publisher"]=> string(1) "1" } 

the key is :int(1) 
the value is :array(8) { [0]=> string(1) "2" ["id"]=> string(1) "2" [1]=> string(4) "2222" ["ISBN"]=> string(4) "2222" [2]=> string(6) "t22222" ["title"]=> string(6) "t22222" [3]=> string(1) "1" ["publisher"]=> string(1) "1" } 

the key is :int(2) 
the value is :array(8) { [0]=> string(1) "3" ["id"]=> string(1) "3" [1]=> string(4) "3333" ["ISBN"]=> string(4) "3333" [2]=> string(6) "t33333" ["title"]=> string(6) "t33333" [3]=> string(1) "1" ["publisher"]=> string(1) "1" } 


the key is :int(3) 
the value is :array(8) { [0]=> string(1) "4" ["id"]=> string(1) "4" [1]=> string(4) "4444" ["ISBN"]=> string(4) "4444" [2]=> string(6) "t44444" ["title"]=> string(6) "t44444" [3]=> string(1) "2" ["publisher"]=> string(1) "2" } 

the key is :int(4) 
the value is :array(8) { [0]=> string(1) "5" ["id"]=> string(1) "5" [1]=> string(4) "5555" ["ISBN"]=> string(4) "5555" [2]=> string(6) "t55555" ["title"]=> string(6) "t55555" [3]=> string(1) "2" ["publisher"]=> string(1) "2" } 

the key is :int(5) 
the value is :array(8) { [0]=> string(1) "6" ["id"]=> string(1) "6" [1]=> string(4) "6666" ["ISBN"]=> string(4) "6666" [2]=> string(6) "t66666" ["title"]=> string(6) "t66666" [3]=> string(1) "2" ["publisher"]=> string(1) "2" } 


the key is :int(6) 
the value is :array(8) { [0]=> string(1) "7" ["id"]=> string(1) "7" [1]=> string(4) "7777" ["ISBN"]=> string(4) "7777" [2]=> string(6) "t77777" ["title"]=> string(6) "t77777" [3]=> string(1) "3" ["publisher"]=> string(1) "3" } 

the key is :int(7) 
the value is :array(8) { [0]=> string(1) "8" ["id"]=> string(1) "8" [1]=> string(4) "8888" ["ISBN"]=> string(4) "8888" [2]=> string(6) "t88888" ["title"]=> string(6) "t88888" [3]=> string(1) "3" ["publisher"]=> string(1) "3" } 

the key is :int(8) 
the value is :array(8) { [0]=> string(1) "9" ["id"]=> string(1) "9" [1]=> string(4) "9999" ["ISBN"]=> string(4) "9999" [2]=> string(6) "t99999" ["title"]=> string(6) "t99999" [3]=> string(1) "3" ["publisher"]=> string(1) "3" } 

 

将上面的变量申请改为:

$a = new Test1(new Books(), 2, 4);

for循环不用改变(如果改变了就不算是iterator样式了)

输出如下:

the key is :int(3) 
the value is :array(8) { [0]=> string(1) "4" ["id"]=> string(1) "4" [1]=> string(4) "4444" ["ISBN"]=> string(4) "4444" [2]=> string(6) "t44444" ["title"]=> string(6) "t44444" [3]=> string(1) "2" ["publisher"]=> string(1) "2" } 

the key is :int(4) 
the value is :array(8) { [0]=> string(1) "5" ["id"]=> string(1) "5" [1]=> string(4) "5555" ["ISBN"]=> string(4) "5555" [2]=> string(6) "t55555" ["title"]=> string(6) "t55555" [3]=> string(1) "2" ["publisher"]=> string(1) "2" } 

the key is :int(5) 
the value is :array(8) { [0]=> string(1) "6" ["id"]=> string(1) "6" [1]=> string(4) "6666" ["ISBN"]=> string(4) "6666" [2]=> string(6) "t66666" ["title"]=> string(6) "t66666" [3]=> string(1) "2" ["publisher"]=> string(1) "2" } 


the key is :int(6) 
the value is :array(8) { [0]=> string(1) "7" ["id"]=> string(1) "7" [1]=> string(4) "7777" ["ISBN"]=> string(4) "7777" [2]=> string(6) "t77777" ["title"]=> string(6) "t77777" [3]=> string(1) "3" ["publisher"]=> string(1) "3" } 

 

 

 

 

posted on 2012-08-24 13:17  我是正常蛇  阅读(151)  评论(0)    收藏  举报