具體效果可見本站的分頁效果, CSS樣式可根據個人設計感進行更變。
這里我會舉例演示如何使用該類, 如下:

IndexController.php, 在 Action 中寫入如下代碼: " /> 免费国产综合视频在线看,久久精品中文字幕,亚洲薄码区

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

非常好用的Zend Framework分頁類

在這里和大家分享一個非常好用的 Zend Framework 分頁類
 
具體效果可見本站的分頁效果, CSS樣式可根據個人設計感進行更變。
 

這里我會舉例演示如何使用該類, 如下:
 
IndexController.php, 在 Action 中寫入如下代碼:
復制代碼 代碼如下:
protected  $_curPage = 1;      //默認第一頁
const PERPAGENUM     = 4;      //每頁顯示條目數
 
public function indexAction()
{  
    // $this->_blogModel 已實例化 blog Model
    // $rows -> 獲得到所展示數據的總條目數
    $rows = $this->_blogModel->getTotalRows();
     
    if($pageNum = $this->getRequest()->getParam('page')) {
        //如果有值傳入,覆蓋初始的第一頁
        $this->_curPage = $pageNum;
    }
     
    //把數據表中的數據傳到前端
    $this->view->blogInfo = $this->_blogModel->getBlogInfo(
                                self::PERPAGENUM, ($this->_curPage-1)*self::PERPAGENUM
                            );
    //實例化分頁類,并傳到前端
    $this->view->pagebar = $this->displayPageBar($rows);
}
 
private function displayPageBar($totalRows)
{
    $Pager = new Zend_Pagination($totalRows,self::PERPAGENUM);
    return $Pager->getNavigation();
}

models/Blog.php,寫入如下代碼:
復制代碼 代碼如下:
public function getBlogInfo($perPageNum = NULL, $limit = NULL)
{
    return $this->fetchAll('1 = 1', 'blog_id desc', $perPageNum, $limit)
                ->toArray();
}
 
public function getTotalRows($where = '1=1')
{
    return $this->fetchAll($where)->count();
}

index.phtml, 寫入如下代碼:
復制代碼 代碼如下:
<div class="page">
    <!--?php echo $this--->pagebar; ?>
</div>

到這里,就可以看見效果了, 如想追求更好的頁面效果, 請根據個人喜好修改分頁類,這里就不作詳細示例
復制代碼 代碼如下:
class Zend_Pagination
{
    private $_navigationItemCount = 6;        //導航欄顯示導航總頁數
    private $_pageSize            = null;     //每頁項目數
    private $_align               = "right";  //導航欄顯示位置
    private $_itemCount           = null;     //總項目數
    private $_pageCount           = null;     //總頁數
    private $_currentPage         = null;     //當前頁
    private $_front               = null;     //前端控制器
    private $_PageParaName        = "page";   //頁面參數名稱
 
    private $_firstPageString     = "|<<";    //導航欄中第一頁顯示的字符
    private $_nextPageString      = ">>";     //導航欄中前一頁顯示的字符
    private $_previousPageString  = "<<";     //導航欄中后一頁顯示的字符
    private $_lastPageString      = ">>|";    //導航欄中最后一頁顯示的字符
    private $_splitString         = " | ";    //頁數字間的間隔符
 
    public function __construct($itemCount, $pageSize)
    {
        if (!is_numeric($itemCount) || (!is_numeric($pageSize))) {
            throw new Exception("Pagination Error:not Number");
        }
        $this->_itemCount = $itemCount;
        $this->_pageSize  = $pageSize;
        $this->_front     = Zend_Controller_Front::getInstance();
 
        $this->_pageCount = ceil($itemCount/$pageSize);   //總頁數
        $page = $this->_front->getRequest()->getParam($this->_PageParaName);
         
        if (empty($page) || (!is_numeric($page))) {  
            //為空或不是數字,設置當前頁為1
            $this->_currentPage = 1;
        } else {
            if ($page < 1) {
                $page = 1;
            }
            if ($page > $this->_pageCount) {
                $page = $this->_pageCount;
            }
            $this->_currentPage = $page;
        }
    }
 
    public function getCurrentPage()
    {
        return $this->_currentPage;
    }
 
    public function getNavigation()
    {
        $navigation = '<div style="text-align:'.$this->_align.';" class="pagecss">';
         
        //當前頁處于第幾欄分頁
        $pageCote      = ceil($this->_currentPage / ($this->_navigationItemCount - 1)) - 1;  
        //總分頁欄
        $pageCoteCount = ceil($this->_pageCount / ($this->_navigationItemCount - 1));
        //分頁欄中起始頁
        $pageStart     = $pageCote * ($this->_navigationItemCount -1) + 1; 
        //分頁欄中終止頁      
        $pageEnd       = $pageStart + $this->_navigationItemCount - 1;                      
         
        if($this->_pageCount < $pageEnd) {
            $pageEnd   = $this->_pageCount;
        }
         
        $navigation .= "總共: {$this->_itemCount} 條 共 {$this->_pageCount} 頁/n  ";
         
        if($pageCote > 0) {           //首頁導航
            $navigation .= '<a href="'.$this->createHref(1)
                           ." /"="">$this->_firstPageString</a> ";
        }
        if($this->_currentPage != 1) {       //導航
            $navigation .= '<a href="'.$this->createHref($this->_currentPage-1);
            $navigation .= " /"="">$this->_previousPageString</a> ";
        }else{
            $navigation .= $this->_previousPageString . ' ';
        }
        
        while ($pageStart <= $pageEnd)      //構造數字導航區
        {
            if ($pageStart == $this->_currentPage) {
                $navigation .= "<b>$pageStart</b>" . $this->_splitString;
            } else {
                $navigation .= '<a href="'.$this->createHref($pageStart)
                               ." /"="">$pageStart</a>"
                               . $this->_splitString;
            }
            $pageStart++;
        }
         
        if($this->_currentPage != $this->_pageCount) {   //導航
            $navigation .= ' <a href="'
                           . $this->createHref($this->_currentPage+1)
                           . " /"="">$this->_nextPageString</a> ";
        }else{
            $navigation .= $this->_nextPageString;
        }
        
        if ($pageCote < $pageCoteCount-1) {               //未頁導航
            $navigation .= '<a href="'
                           . $this->createHref($this->_pageCount)
                           . " /"="">$this->_lastPageString</a> ";
        }
 
        $navigation .= ' 到 <select onchange="window.location=/' '
                       . $this->createHref()
                       . '/'+this.options[this.selectedIndex].value;">';
         
        for ($i=1;$i<=$this->_pageCount;$i++){
            if ($this->getCurrentPage()==$i){
               $selected = "selected";
            } else {
               $selected = "";
            }
            $navigation .= '<option value=" . $i . " '="" .="" $selected="">'
                           . $i
                           . '</option>';
        }
        $navigation .= '</select>';
        $navigation .= " 頁</div>";
        return $navigation;
    }
 
    public function getNavigationItemCount()
    {
        return $this->_navigationItemCount;
    }
 
    public function setNavigationItemCoun($navigationCount)
    {
        if(is_numeric($navigationCount)) {
            $this->_navigationItemCount = $navigationCount;
        }
    }
 
    public function setFirstPageString($firstPageString)
    {
        $this->_firstPageString = $firstPageString;
    }
 
    public function setPreviousPageString($previousPageString)
    {
        $this->_previousPageString = $previousPageString;
    }
 
    public function setNextPageString($nextPageString)
    {
        $this->_nextPageString = $nextPageString;
    }
 
    public function setLastPageString($lastPageString)
    {
        $this->_lastPageString = $lastPageString;
    }
 
    public function setAlign($align)
    {
        $align = strtolower($align);
        if ($align == "center") {
            $this->_align = "center";
        } elseif ($align == "right") {
            $this->_align = "right";
        } else {
            $this->_align = "left";
        }
    }
    
    public function setPageParamName($pageParamName)
    {
        $this->_PageParaName = $pageParamName;
    }
 
    public function getPageParamName()
    {
        return $this->_PageParaName;
    }
 
    private function createHref($targetPage = null)
    {
        $params     = $this->_front->getRequest()->getParams();
        $module     = $params["module"];
        $controller = $params["controller"];
        $action     = $params["action"];
 
        $targetUrl = $this->_front->getBaseUrl()
                     . "/$module/$controller/$action";
                      
        foreach ($params as $key => $value)
        {
            if($key != "controller" && $key != "module"
               && $key != "action" && $key != $this->_PageParaName) {
                $targetUrl .= "/$key/$value";
            }
        }
        if (isset($targetPage)) {                //指定目標頁
            $targetUrl .= "/$this->_PageParaName/$targetPage";
        } else {
            $targetUrl .= "/$this->_PageParaName/";
        }
        return $targetUrl;
    }
}

這里再簡單回顧下 Mysql 中的 limit offset
 
假設數據庫表 blog 存在 13 條數據。

語句1:select * from blog limit 9, 4
語句2:select * from blog limit 4 offset 9

//語句1和2均返回表 blog 的第 10、11、12、13 行
//語句1中的 9 表示從表的第十行開始, 返回 4 行
//語句2中的 4 表示返回 4 行,offset 9 表示從表的第十行開始

如下語句顯示分頁效果:

語句3:select * from blog limit ($this->_curPage - 1)* self::PERPAGENUM, self::PERPAGENUM;
語句4:select * from blog limit self::PERPAGENUM offset ($this->_curPage - 1) * self::PERPAGENUM;

php技術非常好用的Zend Framework分頁類,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产成+人欧美+综合在线观看 | 亚洲黄色片免费看 | 1a级毛片免费观看 | 免费看毛片的网址 | h片下载地址 | 日韩欧美一区二区中文字幕 | 日韩精品欧美在线视频在线 | 亚洲AV蜜桃永久无码精品红樱桃 | 俄罗斯15一16处交 | 国产99久久久欧美黑人刘玥 | 忘忧草日本在线社区WWW电影 | 91涩涩视频 | 色哦色哦哦色天天综合 | 亚洲高清一区二区三区电影 | 欧美123区| 亚洲成AV人片一区二区不卡 | 99在线精品国自产拍不卡 | 欧美 亚洲 日韩 中文2019 | 日本一卡2卡3卡四卡精品网站 | 肉奴隷 赤坂丽在线播放 | 久久精品影视 | 国产一区在线观看免费 | 1234成人网 | 熟女久久久久久久久久久 | 丰满少妇69激情啪啪无码 | 人淫阁 | 蝴蝶中文综合娱乐网2 | 俄罗斯美女破处 | 国产手机在线精品 | 伦理片在线3348 | 一线高清视频在线播放 | 免费看黄色小说 | 国产亚洲精品免费视频 | 在线观看亚洲免费视频 | 伊人久久久久久久久香港 | 国产电影午夜成年免费视频 | 特大巨黑人吊性xxxxgay | 三级网址在线观看 | 国产免费人成在线视频视频 | 国产第一页在线视频 | 国产亚洲国际精品福利 |