html5中文学习网

您的位置: 首页 > 网络编程 > PHP编程 » 正文

PHP设计模式之迭代器模式_PHP教程_编程技术

[ ] 已经帮助:人解决问题

迭代器:迭代器设计模式可帮助构造特定对象, 那些对象能够提供单一标准接口循环或迭代任何类型的可计数数据。
处理需要遍历的可计数数据时, 最佳的解决办法是创建一个基于迭代器设计模式的对象。VGLHTML5中文学习网 - HTML5先行者学习网

  1. <?php   
  2. class CD {   
  3.        
  4.     public $band  = "";   
  5.     public $title = "";   
  6.     public $trackList = array();   
  7.        
  8.     public function __construct($band$title) {   
  9.         $this->band  = $band;   
  10.         $this->title = $title;   
  11.     }   
  12.        
  13.     public function addTrack($track) {   
  14.         $this->trackList[] = $track;   
  15.     }   
  16. }   
  17.    
  18. class CDSearchByBandIterator implements Iterator {   
  19.        
  20.     private $_CDs   = array();   
  21.     private $_valid = FALSE;   
  22.        
  23.     public function __construct($bandName) {   
  24.         $db = mysql_connect("localhost""root""root");   
  25.         mysql_select_db("test");   
  26.            
  27.         $sql  = "select CD.id, CD.band, CD.title, tracks.tracknum, tracks.title as tracktitle ";   
  28.         $sql .= "from CD left join tracks on CD.id = tracks.cid ";   
  29.         $sql .= "where band = '" . mysql_real_escape_string($bandName) . "' ";   
  30.         $sql .= "order by tracks.tracknum";   
  31.            
  32.         $results = mysql_query($sql);   
  33.    
  34.         $cdID = 0;   
  35.         $cd   = NULL;   
  36.            
  37.         while ($result = mysql_fetch_array($results)) {   
  38.             if ($result["id"] !== $cdID) {   
  39.                 if ( ! is_null($cd)) {   
  40.                     $this->_CDs[] = $cd;   
  41.                 }   
  42.                    
  43.                 $cdID = $result['id'];   
  44.                 $cd   = new CD($result['band'], $result['title']);   
  45.             }   
  46.                
  47.             $cd->addTrack($result['tracktitle']);   
  48.         }   
  49.            
  50.         $this->_CDs[] = $cd;   
  51.     }   
  52.        
  53.     public function next() {   
  54.         $this->_valid = (next($this->_CDs) === FALSE) ? FALSE : TRUE;   
  55.     }   
  56.        
  57.     public function rewind() {   
  58.         $this->_valid = (reset($this->_CDs) === FALSE) ? FALSE : TRUE;   
  59.     }   
  60.        
  61.     public function valid() {   
  62.         return $this->_valid;   
  63.     }   
  64.        
  65.     public function current() {   
  66.         return current($this->_CDs);   
  67.     }   
  68.        
  69.     public function key() {   
  70.         return key($this->_CDs);   
  71.     }   
  72. }   
  73.    
  74. $queryItem = "Never Again";   
  75.    
  76. $cds = new CDSearchByBandIterator($queryItem);   
  77.    
  78. print "<h1>Found the Following CDs</h1>";   
  79. print "<table border='1'><tr><th>Band</th><th>Ttile</th><th>Num Tracks</th></tr>";   
  80. foreach ($cds as $cd) {   
  81.     print "<tr><td>{$cd->band}</td><td>{$cd->title}</td><td>";   
  82.     print count($cd->trackList). "</td></tr>";   
  83. }   
  84. print "</table>";   
  85. ?> 

 数据库脚本请参照:http://www.cxybl.com/html/wlbc/Php/2011_1126_9458.htmlVGLHTML5中文学习网 - HTML5先行者学习网

VGLHTML5中文学习网 - HTML5先行者学习网
VGLHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助