|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
完成一个功能齐全的动态站点 迭代器:迭代器设计形式可匡助机关特定对象, 那些对象可以供应单一尺度接口轮回或迭代任何类型的可计数数据。
处置需求遍历的可计数数据时, 最好的处理举措是创立一个基于迭代器设计形式的对象。
- <?php class CD { public $band = ""; public $title = ""; public $trackList = array(); public function __construct($band, $title) { $this->band = $band; $this->title = $title; } public function addTrack($track) { $this->trackList[] = $track; } } class CDSearchByBandIterator implements Iterator { private $_CDs = array(); private $_valid = FALSE; public function __construct($bandName) { $db = mysql_connect("localhost", "root", "root"); mysql_select_db("test"); $sql = "select CD.id, CD.band, CD.title, tracks.tracknum, tracks.title as tracktitle "; $sql .= "from CD left join tracks on CD.id = tracks.cid "; $sql .= "where band = '" . mysql_real_escape_string($bandName) . "' "; $sql .= "order by tracks.tracknum"; $results = mysql_query($sql); $cdID = 0; $cd = NULL; while ($result = mysql_fetch_array($results)) { if ($result["id"] !== $cdID) { if ( ! is_null($cd)) { $this->_CDs[] = $cd; } $cdID = $result['id']; $cd = new CD($result['band'], $result['title']); } $cd->addTrack($result['tracktitle']); } $this->_CDs[] = $cd; } public function next() { $this->_valid = (next($this->_CDs) === FALSE) ? FALSE : TRUE; } public function rewind() { $this->_valid = (reset($this->_CDs) === FALSE) ? FALSE : TRUE; } public function valid() { return $this->_valid; } public function current() { return current($this->_CDs); } public function key() { return key($this->_CDs); } } $queryItem = "Never Again"; $cds = new CDSearchByBandIterator($queryItem); print "<h1>Found the Following CDs</h1>"; print "<table border='1'><tr><th>Band</th><th>Ttile</th><th>Num Tracks</th></tr>"; foreach ($cds as $cd) { print "<tr><td>{$cd->band}</td><td>{$cd->title}</td><td>"; print count($cd->trackList). "</td></tr>"; } print "</table>"; ?>
复制代码 数据库剧本请参照:http://www.cxybl.com/html/wlbc/Php/2011_1126_9458.html
在相册系统的开发上,因为采用的是团队分工合作方式,更让我明白了在一个团队之中,团队成员之间的交流沟通的重要性,如果没有很好的沟通交流,成员之间的任务没有分配好。 |
|