日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 迭代设计模式 -- BookList -- 分页

發布時間:2025/3/15 php 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 迭代设计模式 -- BookList -- 分页 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、SQL語句

View Code -- -- 數據庫: `booklist` ---- ---------------------------------------------------------- -- 表的結構 `authors` --CREATE TABLE IF NOT EXISTS `authors` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(20) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;-- -- 轉存表中的數據 `authors` --INSERT INTO `authors` (`id`, `name`) VALUES (1, 'admin'), (2, 'user');-- ---------------------------------------------------------- -- 表的結構 `books` --CREATE TABLE IF NOT EXISTS `books` (`id` int(11) NOT NULL AUTO_INCREMENT,`ISBN` varchar(20) CHARACTER SET utf8 DEFAULT NULL,`title` varchar(50) CHARACTER SET utf8 DEFAULT NULL,`publisher` int(11) DEFAULT NULL,PRIMARY KEY (`id`),UNIQUE KEY `ISBN` (`ISBN`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=50 ;-- -- 轉存表中的數據 `books` --INSERT INTO `books` (`id`, `ISBN`, `title`, `publisher`) VALUES (1, '12345678', '測試書本1', 1), (2, '12345679', '測試書本2', 1), (3, '0.72667894766357', '測試書本3', 1), (4, '0.1224353342526', '測試書本4', 1), (5, '0.43213985900594', '測試書本5', 1), (6, '0.79339332300554', '測試書本6', 1), (7, '0.67054706874354', '測試書本7', 1), (8, '0.97255540543474', '測試書本8', 1), (9, '0.8511358516767', '測試書本9', 1), (10, '0.33801307281294', '測試書本10', 1), (11, '0.13665783976825', '測試書本11', 1), (12, '0.66925001113606', '測試書本12', 1), (13, '0.93627656710919', '測試書本13', 1), (14, '0.67363283287141', '測試書本14', 1), (15, '0.55933449376313', '測試書本15', 1), (16, '0.77577400093505', '測試書本16', 1), (17, '0.2008665541195', '測試書本17', 1), (18, '0.67701079852601', '測試書本18', 1), (19, '0.78245436100518', '測試書本19', 1), (20, '0.88123944018152', '測試書本20', 1), (21, '0.058834148625689', '測試書本21', 1), (22, '0.65045245331754', '測試書本22', 1), (23, '0.075759851444289', '測試書本23', 1), (24, '0.42744192800246', '測試書本24', 1), (25, '0.9099301164131', '測試書本25', 1), (26, '0.26732482879173', '測試書本26', 1), (27, '0.60683382545303', '測試書本27', 1), (28, '0.23219467162359', '測試書本28', 1), (29, '0.34047191249251', '測試書本29', 1), (30, '0.0057755783254053', '測試書本30', 1), (31, '0.0074621848831532', '測試書本31', 1), (32, '0.019984220173195', '測試書本32', 1), (33, '0.077534576950161', '測試書本33', 1), (34, '0.32772025496487', '測試書本34', 1), (35, '0.40599757470749', '測試書本35', 1), (36, '0.046827139376502', '測試書本36', 1), (37, '0.016143003493681', '測試書本37', 1), (38, '0.94023363007254', '測試書本38', 1), (39, '0.65273917061532', '測試書本39', 1), (40, '0.44299499359261', '測試書本40', 1), (41, '0.25675748685073', '測試書本41', 1), (42, '0.95480248420947', '測試書本42', 1), (43, '0.0037399912287854', '測試書本43', 1), (44, '0.15429253424918', '測試書本44', 1), (45, '0.76024272829317', '測試書本45', 1), (46, '0.33833606945196', '測試書本46', 1), (47, '0.41095219311393', '測試書本47', 1), (48, '0.039752787947424', '測試書本48', 1), (49, '0.96590739112898', '測試書本49', 1);-- ---------------------------------------------------------- -- 表的結構 `book_authors` --CREATE TABLE IF NOT EXISTS `book_authors` (`bookid` int(11) NOT NULL DEFAULT '0',`authorid` int(11) NOT NULL DEFAULT '0',PRIMARY KEY (`bookid`,`authorid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;-- -- 轉存表中的數據 `book_authors` --INSERT INTO `book_authors` (`bookid`, `authorid`) VALUES (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), (7, 2), (8, 2), (9, 2), (10, 2), (11, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 2), (17, 2), (18, 2), (19, 2), (20, 2), (21, 2), (22, 2), (23, 2), (24, 2), (25, 2), (26, 2), (27, 1), (28, 1), (29, 1), (30, 1), (31, 1), (32, 1), (33, 1), (34, 1), (35, 1), (36, 1), (37, 1), (38, 2), (39, 2), (40, 2), (41, 2), (42, 2), (43, 2), (44, 2), (45, 1), (46, 1), (47, 1), (48, 1), (49, 2);-- ---------------------------------------------------------- -- 表的結構 `publisher` --CREATE TABLE IF NOT EXISTS `publisher` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(20) CHARACTER SET utf8 DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;-- -- 轉存表中的數據 `publisher` --INSERT INTO `publisher` (`id`, `name`) VALUES (1, '清華大學'), (2, '北京大學');

二、視圖 show.php

<?phprequire_once dirname(__FILE__).'/booklist.class.php';require_once dirname(__FILE__).'/page.class.php';$dbConn = new mysqli('localhost','root','123456','booklist');$dbConn->query("set names utf8");$booksPerPage = 10;$page = isset($_GET['page']) ? $_GET['page'] : 1;$bookList = new BookList($dbConn);$bookPage = new Page($bookList,$page,$booksPerPage); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title> 圖書俱樂部 </title> </head> <body><p>Showing books</p><?phpprintf('%d-%d of %d',$bookPage->getFirstIndex(),$bookPage->getLastIndex(),count($bookList));?><table><thead><th>Title</th><th>Author</th><th>Publisher</th></thead><tbody><?phpforeach($bookPage as $key => $book){printf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>',$book['title'],$book['author'],$book['publisher']);}?></tbody></table><ul><?php$totalPages = ceil(count($bookList) / $booksPerPage);if($page > 1){printf('<li><a href="?page=%d">Prev</a></li>',$page-1);}for($i = 1 ; $i <= $totalPages ; $i++){if( $i == $page){printf('<li>%d</li>', $i);} else {printf('<li><a href="?page=%d">%d</a></li>',$i,$i);}}if($page < $totalPages){printf('<li><a href="?page=%d">Next</a></li>', $page+1);}?></ul> </body> </html> <!-- //系統執行步驟 一、實例化bookList類和page類,bookList實例連接數據庫,bookPage實例傳入bookList實例,當前頁,偏移量 二、外部迭代類bookPage實例調用了rewind()方法,通過getInnerIterator()方法獲取內部迭代實例,調用了bookList實例的seek方法 三、bookList實例的seek方法設置了當前索引并獲取索引內容 四、bookPage實例的foreach執行了valid()方法,該方法調用了bookList實例的key方法,返回當前迭代實例的key 五、bookPage實例通過驗證,執行current()方法,該方法調用了bookList實例的current方法,返回當前內容 六、bookPage實例接著key()方法,該方法調用了bookList實例的key方法,返回當前主鍵 七、bookPage實例進入next方法,該方法調用了bookList實例的next方法,指針下移 -->

三、BookList類 booklist.class.php

<?php class BookList implements Iterator,Countable,SeekableIterator,ArrayAccess {private $pages = array();private $currentPag = 1;private $database = null;private $booksPerPage = 10;private $totalBooks = 0;private $deletions = array();public function __construct($database){$this->database = $database;if($result = $this->database->query("select count(*) from books")){if($row = $result->fetch_row()){$this->totalBooks = $row[0];}}}/* 迭代接口 */function rewind(){$this->currentPage = 1;if( array_key_exists(1,$this->pages) ){reset($this->pages[$i]);}}function current(){return current($this->touchPage());}function key(){return key($this->touchPage()) + $this->booksPerPage * ($this->currentPage -1);}function next(){$page = & $this->touchPage();next($page);if( key($page) === null && count($page) == $this->booksPerPage){$this->currentPage++;$page = & $this->touchPage();reset($page);}return current($page);}function valid(){$page = & $this->touchPage();return (key($page) !== null);}//查詢數據private function & touchPage($pageNo = false){if($pageNo === false){$pageNo = $this->currentPage;}if(! array_key_exists($pageNo,$this->pages)){if( $pageNo > ceil($this->count() / $this->booksPerPage)){$this->pages[$pageNo] = array();}else{$start = ($pageNo-1)* $this->booksPerPage + $this->getAdjustmentForPage($pageNo);$sql = "select `ISBN`,title,(select group_concat(`name`) from `book_authors` as t2 join `authors` as t3 on t2.authorid = t3.id where t2.bookid = t1.id) as `author`,`t4`.`name` as publisher from `books` as t1 join `publisher` as t4 on t1.publisher = t4.id limit $start,{$this->booksPerPage}";$result = $this->database->query($sql);while ($row = $result->fetch_array(MYSQLI_ASSOC)){$temp_array[] = $row;}$this->pages[$pageNo] = $temp_array;}}$temp = & $this->pages[$pageNo];return $temp;}/* 計數接口 */public function count(){return $this->totalBooks;}/* 索引接口 */public function seek($index){if($index < 0 || $index > $this->totalBooks){throw new OutOfBoundsException();}$this->currentPage = (int) floor($index / $this->booksPerPage) +1;$page = & $this->touchPage();reset($page);for($i = $index % $this->booksPerPage; $i>0 ; $i--){next($page);}}/* 數組訪問接口*/public function offsetExists($offset){echo 'offsetExists';return ( $offset > 0 && $offset < count($this) );}public function offsetGet($offset){echo 'offsetGet';$pageOfOffset = (int)floor($offset / $this->booksPerPage) + 1;$page = & $this->touchPage($pageOfOffset);return $page[$offset / $this->booksPerPage];}public function offsetSet($offset,$newValue){echo 'offsetSet'; $pageOfOffset = (int)floor($offset / $this->booksPerPage) + 1;$page = & $this->touchPage($pageOfOffset);$page[$offset % $this->booksPerPage] = $newValue;}public function offsetUnset($offset){echo 'offsetUnset'; $pageOfOffset = (int)floor($offset / $this->booksPerPage) + 1;$page = & $this->touchPage($pageOfOffset);$this->deletions[$pageOfOffset] ++;ksort($this->deletions);unset($page[$offset % $this->booksPerPage]);$page = array_values($page);while(is_array($this->pages[$pageOfOffset+1])){$this->pages[$pageOfOffset][] = array_shift($this->pages[++$pageOfOffset]);}$record = ($pageOfOffset -1)*$this->booksPerPage + count($this->pages[$pageOfOffset]) + $this->getAdjustmentForPage($pageOfOffset);if($result = $this->database->query("select * from books limit $record,1")){$this->pages[$pageOfOffset][] = $result->fetch_object();}$this->totalBooks --;}private function getAdjustmentForPage($pageNo){$adjust = 0;for(reset($this->deletions);key($this->deletions) !== null && key($this->deletions) < $pageNo;next($this->deletions)){$adjust += current($this->deletions);}return $adjust;} } ?>

四、Page類 page.class.php

<?php class Page extends IteratorIterator {private $page;private $currentItem;public $itemsPerPage;public function __construct(Iterator $iterator,$page,$itemsPerPage){parent::__construct($iterator);$this->page = $page;$this->itemsPerPage = $itemsPerPage;$this->rewind();}public function getFirstIndex(){return ($this->page-1) * $this->itemsPerPage;}public function getLastIndex(){return $this->getFirstIndex() + $this->itemsPerPage;}function valid(){return ( $this->currentItem != $this->itemsPerPage && $this->getInnerIterator()->key() !== null);}function rewind(){$currentItem = 0;$this->getInnerIterator()->seek( $this->getFirstIndex() );}function next(){if( $this->currentItem < $this->itemsPerPage){$this->currentItem ++;$this->getInnerIterator()->next();}} function current(){return ($this->currentItem != $this->itemsPerPage ? $this->getInnerIterator()->current() : null);}function key(){return ($this->currentItem != $this->itemsPerPage ? $this->getInnerIterator()->key() : null);}} ?>

?

代碼打包下載:PHP迭代設計模式--booklist.zip

?

?

轉載于:https://www.cnblogs.com/linzhenjie/archive/2013/03/14/2958639.html

總結

以上是生活随笔為你收集整理的php 迭代设计模式 -- BookList -- 分页的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。