溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

有哪些非常好用的Zend Framework分頁類

發(fā)布時(shí)間:2021-09-29 14:01:41 來源:億速云 閱讀:131 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“有哪些非常好用的Zend Framework分頁類”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

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

這里我會(huì)舉例演示如何使用該類, 如下:
 
IndexController.php, 在 Action 中寫入如下代碼:

復(fù)制代碼 代碼如下:


protected  $_curPage = 1;      //默認(rèn)第一頁
const PERPAGENUM     = 4;      //每頁顯示條目數(shù)
 
public function indexAction()
{  
    // $this->_blogModel 已實(shí)例化 blog Model
    // $rows -> 獲得到所展示數(shù)據(jù)的總條目數(shù)
    $rows = $this->_blogModel->getTotalRows();
     
    if($pageNum = $this->getRequest()->getParam('page')) {
        //如果有值傳入,覆蓋初始的第一頁
        $this->_curPage = $pageNum;
    }
     
    //把數(shù)據(jù)表中的數(shù)據(jù)傳到前端
    $this->view->blogInfo = $this->_blogModel->getBlogInfo(
                                self::PERPAGENUM, ($this->_curPage-1)*self::PERPAGENUM
                            );
    //實(shí)例化分頁類,并傳到前端
    $this->view->pagebar = $this->displayPageBar($rows);
}
 
private function displayPageBar($totalRows)
{
    $Pager = new Zend_Pagination($totalRows,self::PERPAGENUM);
    return $Pager->getNavigation();
}

models/Blog.php,寫入如下代碼:

復(fù)制代碼 代碼如下:


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, 寫入如下代碼:

復(fù)制代碼 代碼如下:


<div class="page">
    <!--?php echo $this--->pagebar; ?>
</div>

到這里,就可以看見效果了, 如想追求更好的頁面效果, 請(qǐng)根據(jù)個(gè)人喜好修改分頁類,這里就不作詳細(xì)示例

復(fù)制代碼 代碼如下:


class Zend_Pagination
{
    private $_navigationItemCount = 6;        //導(dǎo)航欄顯示導(dǎo)航總頁數(shù)
    private $_pageSize            = null;     //每頁項(xiàng)目數(shù)
    private $_align               = "right";  //導(dǎo)航欄顯示位置
    private $_itemCount           = null;     //總項(xiàng)目數(shù)
    private $_pageCount           = null;     //總頁數(shù)
    private $_currentPage         = null;     //當(dāng)前頁
    private $_front               = null;     //前端控制器
    private $_PageParaName        = "page";   //頁面參數(shù)名稱
 
    private $_firstPageString     = "|<<";    //導(dǎo)航欄中第一頁顯示的字符
    private $_nextPageString      = ">>";     //導(dǎo)航欄中前一頁顯示的字符
    private $_previousPageString  = "<<";     //導(dǎo)航欄中后一頁顯示的字符
    private $_lastPageString      = ">>|";    //導(dǎo)航欄中最后一頁顯示的字符
    private $_splitString         = " | ";    //頁數(shù)字間的間隔符
 
    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);   //總頁數(shù)
        $page = $this->_front->getRequest()->getParam($this->_PageParaName);
         
        if (empty($page) || (!is_numeric($page))) {  
            //為空或不是數(shù)字,設(shè)置當(dāng)前頁為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  class="pagecss">';
         
        //當(dāng)前頁處于第幾欄分頁
        $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) {           //首頁導(dǎo)航
            $navigation .= '<a href="'.$this->createHref(1)
                           ." \"="">$this->_firstPageString</a> ";
        }
        if($this->_currentPage != 1) {       //上一頁導(dǎo)航
            $navigation .= '<a href="'.$this->createHref($this->_currentPage-1);
            $navigation .= " \"="">$this->_previousPageString</a> ";
        }else{
            $navigation .= $this->_previousPageString . ' ';
        }
        
        while ($pageStart <= $pageEnd)      //構(gòu)造數(shù)字導(dǎo)航區(qū)
        {
            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) {   //下一頁導(dǎo)航
            $navigation .= ' <a href="'
                           . $this->createHref($this->_currentPage+1)
                           . " \"="">$this->_nextPageString</a> ";
        }else{
            $navigation .= $this->_nextPageString;
        }
        
        if ($pageCote < $pageCoteCount-1) {               //未頁導(dǎo)航
            $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)) {                //指定目標(biāo)頁
            $targetUrl .= "/$this->_PageParaName/$targetPage";
        } else {
            $targetUrl .= "/$this->_PageParaName/";
        }
        return $targetUrl;
    }
}

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

語句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;

“有哪些非常好用的Zend Framework分頁類”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI