欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

非常好用的Zend Framework分頁類

 更新時間:2014年06月25日 10:38:25   投稿:junjie  
這篇文章主要介紹了非常好用的Zend Framework分頁類,包含控制層、模型層、視圖層及分頁源碼,Css部分請自由發(fā)揮,需要的朋友可以參考下

在這里和大家分享一個非常好用的 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)      //構造數字導航區(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) {   //下一頁導航
            $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;

相關文章

  • Yii框架ACF(accessController)簡單權限控制操作示例

    Yii框架ACF(accessController)簡單權限控制操作示例

    這篇文章主要介紹了Yii框架ACF(accessController)簡單權限控制操作,結合實例形式分析了Yii框架簡單權限控制操作參數設置與使用技巧,需要的朋友可以參考下
    2019-04-04
  • php實現微信和支付寶支付的示例代碼

    php實現微信和支付寶支付的示例代碼

    這篇文章主要介紹了php實現微信和支付寶支付的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • PHP實現驗證碼校驗功能

    PHP實現驗證碼校驗功能

    這篇文章主要為大家詳細介紹了PHP實現驗證碼校驗功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • php中pcntl_fork創(chuàng)建子進程的方法實例

    php中pcntl_fork創(chuàng)建子進程的方法實例

    這篇文章主要介紹了php中pcntl_fork創(chuàng)建子進程的方法實例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • 使用php顯示搜索引擎來的關鍵詞

    使用php顯示搜索引擎來的關鍵詞

    在訪客從搜索引擎而來的第一個頁面上顯示訪客搜索的關鍵詞,根據這個關鍵詞做出一些提高網站交互能力的改變,比如顯示這個關鍵詞相關的其它文章
    2014-02-02
  • YII路徑的用法總結

    YII路徑的用法總結

    這篇文章主要介紹了YII路徑的用法總結,需要的朋友可以參考下
    2014-07-07
  • PHP實現文件上傳操作和封裝

    PHP實現文件上傳操作和封裝

    這篇文章主要為大家詳細介紹了PHP實現文件上傳操作和封裝,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Smarty中常用變量操作符匯總

    Smarty中常用變量操作符匯總

    這篇文章主要介紹了Smarty中常用變量操作符,實例匯總了常見的各種變量操作符,非常具有實用價值,需要的朋友可以參考下
    2014-10-10
  • php jsonp單引號轉義

    php jsonp單引號轉義

    JSONP(JSON with Padding)是一個非官方的協(xié)議,他的實現方式大致就是:讓客戶端決定要回調的Javascript函數名,在第三方服務端將 JSON 數據拼裝到回調函數名中,返回的就是參數為JSON數據的函數調用腳本,瀏覽器加載腳本并執(zhí)行達到獲取第三方數據的目的。
    2014-11-11
  • smarty高級特性之過濾器的使用方法

    smarty高級特性之過濾器的使用方法

    這篇文章主要介紹了smarty高級特性之過濾器的使用方法,結合實例形式分析了smarty過濾器的相關使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-12-12

最新評論