程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> Zend Framework分頁類用法詳解

Zend Framework分頁類用法詳解

編輯:PHP綜合

本文實例講述了Zend Framework分頁類用法。分享給大家供大家參考,具體如下:

1、分頁類Pagination.php,最好是把這個類放在Zend目錄下

class XY_Pagination
{
  private $_navigationItemCount = 10; //導航欄顯示導航總頁數
  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;
    }
  }
  /**
   * 返回當前頁
   * @param int 當前頁
   */
  public function getCurrentPage()
  {
    return $this->_currentPage;
  }
  /**
   * 返回導航欄目
   * @return string 導航html class="PageNavigation"
   */
  public function getNavigation()
  {
    $navigation = '';
    $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 .= '$this->_firstPageString ";
    }
    if($this->_currentPage != 1) //上一頁導航
    {
      $navigation .= '$this->_previousPageString ";
    }
    while ($pageStart <= $pageEnd) //構造數字導航區
    {
      if($pageStart == $this->_currentPage)
      {
        $navigation .= "$pageStart".$this->_splitString;
      }
      else
      {
        $navigation .= '$pageStart".$this->_splitString;
      }
      $pageStart++;
    }
    if($this->_currentPage != $this->_pageCount) //下一頁導航
    {
      $navigation .= ' $this->_nextPageString ";
    }
    if($pageCote < $pageCoteCount-1) //未頁導航
    {
      $navigation .= '$this->_lastPageString ";
    }
    //添加直接導航框
    //$navigation .= '';
    //2008年8月27號補充輸入非正確頁碼後出現的錯誤——begin
    $navigation .= ' ';
    //2008年8月27號補充輸入非正確頁碼後出現的錯誤——end
    $navigation .= " ";
    return $navigation;
  }
  /**
   * 取得導航欄顯示導航總頁數
   *
   * @return int 導航欄顯示導航總頁數
   */
  public function getNavigationItemCount()
  {
    return $this->_navigationItemCount;
  }
  /**
   * 設置導航欄顯示導航總頁數
   *
   * @param int $navigationCount:導航欄顯示導航總頁數
   */
  public function setNavigationItemCoun($navigationCount)
  {
    if(is_numeric($navigationCount))
    {
      $this->_navigationItemCount = $navigationCount;
    }
  }
  /**
   * 設置首頁顯示字符
   * @param string $firstPageString 首頁顯示字符
   */
  public function setFirstPageString($firstPageString)
  {
    $this->_firstPageString = $firstPageString;
  }
  /**
   * 設置上一頁導航顯示字符
   * @param string $previousPageString:上一頁顯示字符
   */
  public function setPreviousPageString($previousPageString)
  {
    $this->_previousPageString = $previousPageString;
  }
  /**
   * 設置下一頁導航顯示字符
   * @param string $nextPageString:下一頁顯示字符
   */
  public function setNextPageString($nextPageString)
  {
    $this->_nextPageString = $nextPageString;
  }
  /**
   * 設置未頁導航顯示字符
   * @param string $nextPageString:未頁顯示字符
   */
  public function setLastPageString($lastPageString)
  {
    $this->_lastPageString = $lastPageString;
  }
  /**
   * 設置導航字符顯示位置
   * @param string $align:導航位置
   */
  public function setAlign($align)
  {
    $align = strtolower($align);
    if($align == "center")
    {
      $this->_align = "center";
    }elseif($align == "right")
    {
      $this->_align = "right";
    }else
    {
      $this->_align = "left";
    }
  }
  /**
   * 設置頁面參數名稱
   * @param string $pageParamName:頁面參數名稱
   */
  public function setPageParamName($pageParamName)
  {
    $this->_PageParaName = $pageParamName;
  }
  /**
   * 獲取頁面參數名稱
   * @return string 頁面參數名稱
   */
  public function getPageParamName()
  {
    return $this->_PageParaName;
  }
  /**
   * 生成導航鏈接地址
   * @param int $targetPage:導航頁
   * @return string 鏈接目標地址
   */
  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;
  }
}
?>

2、在indexController.php中的indexController Function裡面調用:

require_once 'Zend/Pagination.php';
$Users = new Users();
//$rows = $Users->getAdapter()->fetchOne("select count(*) from users where `role`!='admin'"); //recorde count
$rows = $Users->fetchAll("`role`!='admin'")->count(); //查詢記錄總數
$rowsPerPage = 5; //perPage recordes
$curPage = 1;
if($this->_request->getParam('page'))
{
    $curPage = $this->_request->getParam('page');
}
//search data and display
$this->view->users = $Users->fetchAll("`role`!='admin'",'id desc',$rowsPerPage,($curPage-1)*$rowsPerPage)->toArray();
$Pager = new XY_Pagination($rows,$rowsPerPage);
$this->view->pagebar = $Pager->getNavigation();

3、在view中調用分頁更簡單了。

pagebar?>

或者在smarty模板情況下

<{$pagebar}>

更多關於zend相關內容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優秀開發框架總結》、《Yii框架入門及常用技巧總結》、《ThinkPHP入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家基於Zend Framework框架的PHP程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved