Very easy to use Zend Framework paging class_PHP tutorial
WBOY
Release: 2016-07-13 10:26:07
Original
712 people have browsed it
Here I would like to share with you a very useful Zend Framework paging class
The specific effect can be seen in the paging effect of this site. The CSS style can be changed according to personal design sense.
Here I will give an example to demonstrate how to use this class, as follows:
IndexController.php, write the following code in Action:
Copy code The code is as follows:
protected $_curPage = 1; //Default first page
const PERPAGENUM = 4; //Number of items displayed on each page
public function indexAction()
{
// $this->_blogModel has instantiated blog Model
// $rows -> Get the total number of entries of the displayed data
$rows = $this->_blogModel->getTotalRows();
If($pageNum = $this->getRequest()->getParam('page')) {
//If there is a value passed in, overwrite the initial first page
$this->_curPage = $pageNum;
}
//Transmit the data in the data table to the front end
$this->view->blogInfo = $this->_blogModel->getBlogInfo(
self::PERPAGENUM, ($this->_curPage-1)*self::PERPAGENUM
);
//Instantiate the paging class and pass it to the front end
$this->view->pagebar = $this->displayPageBar($rows);
}
private function displayPageBar($totalRows)
{
$Pager = new Zend_Pagination($totalRows,self::PERPAGENUM);
Return $Pager->getNavigation();
}
models/Blog.php, write the following code:
Copy code The code is as follows:
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, write the following code:
Copy code The code is as follows:
pagebar; ?>
At this point, you can see the effect. If you want to pursue better page effects, please modify the paging class according to your personal preferences. I will not give a detailed example here
Copy code The code is as follows:
class Zend_Pagination
{
Private $_navigationItemCount = 6; //The navigation bar displays the total number of navigation pages
private $_pageSize = null; //Number of items per page
private $_align = "right"; //Navigation bar display position
private $_itemCount = null; //Total number of items
private $_pageCount = null; //Total number of pages
Private $_currentPage = null; //Current page
private $_front = null; //Front-end controller
private $_PageParaName = "page"; //Page parameter name
Private $_firstPageString = "|<<"; //Characters displayed on the first page in the navigation bar
private $_nextPageString = ">>"; //Characters displayed on the previous page in the navigation bar
Private $_previousPageString = "<<"; //Characters displayed on the next page in the navigation bar
Private $_lastPageString = ">>|"; //Characters displayed on the last page in the navigation bar
private $_splitString = " | "; //The spacer between page numbers
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();
If (empty($page) || (!is_numeric($page))) {
//If it 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;
}
}
Public function getCurrentPage()
{
return $this->_currentPage;
}
Assume that the database table blog has 13 pieces of data.
Statement 1: select * from blog limit 9, 4
Statement 2: select * from blog limit 4 offset 9
//Statements 1 and 2 both return rows 10, 11, 12, and 13 of the table blog
//The 9 in statement 1 means starting from the tenth row of the table, returning 4 rows
//The 4 in statement 2 means returning 4 rows, and offset 9 means starting from the tenth row of the table
The following statement shows the paging effect:
Statement 3: select * from blog limit ($this->_curPage - 1)* self::PERPAGENUM, self::PERPAGENUM;
Statement 4: select * from blog limit self::PERPAGENUM offset ($this->_curPage - 1) * self::PERPAGENUM;
http://www.bkjia.com/PHPjc/824809.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824809.htmlTechArticleHere we share with you a very easy-to-use Zend Framework paging class. The specific effect can be seen in the paging effect of this site, CSS Styles can be changed to suit personal design sense. Here I will give an example...
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn