<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                當天新聞記錄不斷增加的時候,必然導致新聞列表不的加長以致用戶不能在一屏內顯示完所有內容,致使用戶需要不停的拉動滾動條來獲取更多的內容,這樣無形之中給用戶瀏覽新聞添加了不少障礙;因此對數據進行分頁將是必然。之前做過網站的都應該知道分頁樣式及功能的實現有多種方法,分頁可以根據不同的需要進行定制,但有一個缺點就是開發者基本上都需要自已寫一個分頁類庫來加以調用。ZF2為了減少開發自已類庫的麻煩ZF2類庫本身就已經集成了分頁的類庫,ZF2提供的分頁類庫簡單易用,開發都也可以根據需要重寫分頁類或對分類的CSS樣式進行重新設定;接下來的內容將重點講解ZF2分頁類庫的使用方法。 ### 6.2.1 修改模塊配置文件 打開文件 `/module/Application/config/module.config.php`,對news 路由區段塊進行修改,具體修改內容如下: ~~~ 'news'=>array( 'type'=>'segment', 'options'=>array( 'route'=>'/news[/][:action][[/:id][/page/:page]]', 'constraints'=>array( 'action'=>'[a-zA-Z]*', 'id'=>'[0-9]+', 'page'=>'[0-9]+', ), 'defaults'=>array( 'controller'=>'Application\Controller\News', 'action'=>'index' ), ), ) ~~~ 修改的地址: 'route'=>'/news[/][:action][/:id]' 修改為 'route'=>'/news[/][:action][[/:id][/page/:page]]' 此處修改的主要作用是為使用路由能匹配出/page/n 這樣的分頁鏈接路徑 添加page'=>'[0-9]+' 路由正則區別規則 ### 6.2.2 修改模型文件 打開文件 `/module/Application/src/Model/NewsTable.php`,修改public function fetchAll(){}函數,具體內容如下: ~~~ public function fetchAll($paginated=false) { if($paginated){ $select = new Select('news'); $rs = new ResultSet(); $rs->setArrayObjectPrototype(new News()); $pageAdapter = new DbSelect($select,$this->tableGateway->getAdapter(),$rs); $paginator = new Paginator($pageAdapter); return $paginator; } $resultSet = $this->tableGateway->select(); return $resultSet; } ~~~ 代碼解釋: if($paginated){} 判斷是否使用分頁 $select = new Select('news'); 實例化一個 select ,對指定表進行操作 $rs = new ResultSet(); 實例化一個結果集,用來保存查詢結果 $rs->setArrayObjectPrototype(new News()); 設置結果集的操作屬性 $pageAdapter = new DbSelect($select,$this->tableGateway->getAdapter(),$rs); 實例化一個DbSelect,并通過數據網關及select來對數據庫進行操作,并將最終結果傳遞到$rs結果集中 $paginator = new Paginator($pageAdapter); 實例化一個分頁導航,并將DbSelect 傳遞過去 return $paginator; 返回分頁導航實例 ### 6.2.3 修改控制器文件 打開文件 `/module/Application/src/Application/Controller/NewsController.php`,對`public function listAction(){}` 方法進行修改,具體內容如下: ~~~ public function listAction(){ $paginator = $this->getNewsTalbe()->fetchAll(true); $paginator->setCurrentPageNumber((int)$this->params()->fromRoute('page',1)); $paginator->setItemCountPerPage(5); return new ViewModel(array('paginator'=>$paginator)); } ~~~ 代碼解釋: $paginator = $this->getNewsTalbe()->fetchAll(true); 表示使用分頁技術進行操作 $paginator->setCurrentPageNumber((int)$this->params()->fromRoute('page',1)); 設置當前頁,如果不存在頁面則默認設置為第一頁 $paginator->setItemCountPerPage(5);設置每個分頁將顯示的記錄行數 return new ViewModel(array('paginator'=>$paginator)); 將分頁導航對象返回給模板調用 ### 6.2.4 添加分頁導航模板 添加文件 `/module/Application/view/application/partial/parginator.phtml`, 具體內容如下: ~~~ <?php if ($this->pageCount): ?> <div class="pagination pagination-centered"> <ul> <!-- Previous page link --> <?php if (isset($this->previous)): ?> <li> <a href="<?php echo $this->url($this->route) . $this->action; ?>/page/<?php echo $this->previous;?>"><<</a> </li> <?php else: ?> <li class="disabled"> <a href="#"> << </a> </li> <?php endif; ?> <?php foreach ($this->pagesInRange as $page): ?> <?php if ($page != $this->current): ?> <li> <a href="<?php echo $this->url($this->route) . $this->action; ?>/page/<?php echo $page; ?>"> <?php echo $page; ?> </a> </li> <?php else: ?> <li class="active"> <a href="#"><?php echo $page; ?></a> </li> <?php endif; ?> <?php endforeach; ?> <?php if (isset($this->next)): ?> <li> <a href="<?php echo $this->url($this->route) . $this->action; ?>/page/<?php echo $this->next; ?>"> >> </a> </li> <?php else: ?> <li class="disabled"> <a href="#"> >> </a> </li> <?php endif; ?> </ul> </div> <?php endif; ?> ~~~ 代碼解釋: if ($this->pageCount) 判斷分頁數量決定是否顯示分頁導航 if (isset($this->previous)) 判斷是否有上一頁 <a href="<?php echo $this->url($this->route) . $this->action; ?>/page/<?php echo $this->previous;?>"><<</a>上一頁鏈接 foreach ($this->pagesInRange as $page) 循環首頁鏈接頁碼 <a href="<?php echo $this->url($this->route) . $this->action; ?>/page/<?php echo $page; ?>"><?php echo $page; ?></a>生成各頁的導航鏈接 if (isset($this->next)) 判斷是否有下一頁 <a href="<?php echo $this->url($this->route) . $this->action; ?>/page/<?php echo $this->next; ?>">>></a>下一頁鏈接 ### 6.2.4 修改新聞列表模板 打開文件 `/module/Appliction/view/application/news/list.phtml`,在此文件末尾添加收下內容: ~~~ <?php echo $this->paginationControl($this->paginator,'sliding',array('application/partial/paginator.phtml','News'),array('route'=>'news','action'=>'list')); ?> ~~~ 此內容的主要功能是將分頁模板輸出。 經過以上內容的添加修改整合后,現在可以通過 http://localhost/news/list 看到新的新聞列表頁,與之前唯一的不同之處就是有分頁導航條了,各個可能點擊分頁的頁面數字對各個頁面進行切換顯示。結果如下: ~~~ header Title Content Add news First news This is the first news Edit Delete Second news This is the second news Edit Delete Third news This is the third news Edit Delete fourth news This is the fourth news Edit Delete Fifth news This is the fifth news Edit Delete << 1 2 >> footer ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看