Zend Framework Pagination Class usage detailed _php example

Source: Internet
Author: User
Tags smarty template zend zend framework

The examples in this article describe the Zend Framework page-class usage. Share to everyone for your reference, specific as follows:

1, pagination class pagination.php, it is best to put this class in the Zend directory

Class Xy_pagination {Private $_navigationitemcount = 10;//navigation bar displays the total number of navigation pages private $_pagesize = NULL;//per page items private $ _align = "right"; Navigation bar display location private $_itemcount = null; Total number of items private $_pagecount = null; Total pages Private $_currentpage = null; Current page Private $_front = null; Front Controller Private $_pageparaname = "page"; Page parameter name private $_firstpagestring = "|<<"; The character displayed on the first page of the navigation bar private $_nextpagestring = ">>"; The character displayed on the previous page in the navigation bar private $_previouspagestring = "<<"; The character that appears on the next page of the navigation bar private $_lastpagestring = ">>|";
         The character that is displayed on the last page of the navigation bar private $_splitstring = "|"; Spacer/Public Function __construct ($itemCount, $pageSize) between page numbers {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);
   Total pages $page = $this->_front->getrequest ()->getparam ($this->_pageparaname); if (Empty ($page) | | (!is_numeric ($page)))
    is empty or not a number, set the current page to 1 {$this->_currentpage = 1;
      else {if ($page < 1) $page = 1;
      if ($page > $this->_pagecount) $page = $this->_pagecount;
    $this->_currentpage = $page; 
  }/** * Back to current page * @param int Current page */Public Function getcurrentpage () {return $this->_currentpage; /** * Return to navigation column * @return string navigation html class= "pagenavigation" * * * Public Function Getnavigation () {$
    Navigation = '; $pageCote = ceil ($this->_currentpage/($this->_navigationitemcount-1))-1; The current page is in the first column $pageCoteCount = ceil ($this->_pagecount/($this->_navigationitemcount-1)); Total page Bar $pageStart = $pageCote * ($this->_navigationitemcount-1) + 1; Page Bar Start Page $pageEnd = $pageStart + $this->_navigationitemcount-1; Stop page in page break if ($this->_PageCount < $pageEnd) {$pageEnd = $this->_pagecount;
    $navigation. = "Total: {$this->_itemcount} {$this->_pagecount}] \ n";
    if ($pageCote > 0)//home navigation {$navigation. = ' $this->_firstpagestring ';
    } if ($this->_currentpage!= 1)//Previous page navigation {$navigation. = ' $this->_previouspagestring '; while ($pageStart <= $pageEnd)//construct the digital navigation area {if ($pageStart = = $this->_currentpage) {$na
      Vigation. = "$pageStart". $this->_splitstring;
      else {$navigation. = ' $pageStart '. $this->_splitstring;
    } $pageStart + +; 
    } if ($this->_currentpage!= $this->_pagecount)//Next page navigation {$navigation. = ' $this->_nextpagestring ';
    } if ($pageCote < $pageCoteCount-1)//pager {$navigation. = ' $this->_lastpagestring ';
    //Add direct navigation box//$navigation. = ';
    August 27, 2008 Add errors that occur after you enter a--begin page number $navigation. = '; 2008 year August 27 add errors that occur after you enter the incorrect page number--end $navigation. = "";
  return $navigation;  /** * Get navigation bar display navigation Total pages * * @return int navigation bar display navigation total pages/Public function Getnavigationitemcount () {return
  $this->_navigationitemcount; /** * Set navigation bar display navigation Total pages * * @param int $navigationCount: Navigation bar Display navigation total pages/Public function Setnavigationitemcoun ($
    Navigationcount) {if (Is_numeric ($navigationCount)) {$this->_navigationitemcount = $navigationCount; }/** * Settings first display character * @param string $firstPageString first display character/public function setfirstpagestring ($firstP
  agestring) {$this->_firstpagestring = $firstPageString; /** * Set previous page navigation display character * @param string $previousPageString: Previous page display character/public function setpreviouspagestring ($pre
  viouspagestring) {$this->_previouspagestring = $previousPageString; /** * Set next page navigation display character * @param string $nextPageString: Next page display character/public function setnextpagestring ($nextPageStr
  ing{$this->_nextpagestring = $nextPageString; /** * Set the setlastpagestring display character * @param string $nextPageString: no page display character */Public function ($lastPageStrin
  g) {$this->_lastpagestring = $lastPageString; /** * Set navigation character display position * @param string $align: Navigation location/Public function setalign ($align) {$align = Strtolowe
    R ($align);
    if ($align = = "Center") {$this->_align = "center";
    }elseif ($align = = "Right") {$this->_align = ' right ';
    }else {$this->_align = "Left"; /** * Set Page parameter name * @param string $pageParamName: Page parameter name/Public function Setpageparamname ($pageParamName
  {$this->_pageparaname = $pageParamName; /** * Get page parameter name * @return String page parameter name */Public Function Getpageparamname () {return $this->_page
  Paraname; /** * Generate navigation link address * @param int $targetPage: Navigation page * @return string link destination address/private function createhref ($targ Etpage = 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))//Specify target page $targetUrl. = "/$this->_pageparaname/$targetPage";
    else $targetUrl. = "/$this->_pageparaname/";
  return $TARGETURL;

 }}?>

2, in the indexcontroller.php in the Indexcontroller function inside call:

Require_once ' zend/pagination.php ';
$Users = new Users ();
$rows = $Users->getadapter ()->fetchone ("SELECT count (*) from the Users where ' role '!= ' admin '); Recorde count
$rows = $Users->fetchall ("' Role '!= ' admin ')->count ();//Total number of query records
$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. It's simpler to call pagination in view.

Pagebar?>

Or in the case of a smarty template

<{$pagebar}>

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.