Very Useful Zend Framework Paging Class
- 2021-07-06 10:16:15
- OfStack
Here to share with you a very useful Zend Framework paging class
Specific effects can be seen in this site paging effect, CSS style can be changed according to personal design sense.
Here I will illustrate how to use this class, as follows:
IndexController. php, write the following code in Action:
protected $_curPage = 1; // Default number 1 Page
const PERPAGENUM = 4; // Number of entries displayed per page
public function indexAction()
{
// $this->_blogModel Instantiated blog Model
// $rows -> Get the total number of entries to the displayed data
$rows = $this->_blogModel->getTotalRows();
if($pageNum = $this->getRequest()->getParam('page')) {
// If a value is passed in, override the initial 1 Page
$this->_curPage = $pageNum;
}
// Pass 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:
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:
<div class="page">
<!--?php echo $this--->pagebar; ?>
</div>
To here, you can see the effect, if you want to pursue a better page effect, please modify the paging class according to your personal preferences, here is not a detailed example
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 projects
private $_pageCount = null; // Total pages
private $_currentPage = null; // Current page
private $_front = null; // Front-end controller
private $_PageParaName = "page"; // Page parameter name
private $_firstPageString = "|<<"; // In the navigation bar 1 Characters displayed on the page
private $_nextPageString = ">>"; // Navigation bar center front 1 Characters displayed on the page
private $_previousPageString = "<<"; // Navigation bar center back 1 Characters displayed on the page
private $_lastPageString = ">>|"; // The last in the navigation bar 1 Characters displayed on the page
private $_splitString = " | "; // 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();
$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, sets 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;
}
public function getNavigation()
{
$navigation = '<div style="text-align:'.$this->_align.';" class="pagecss">';
// What column is the current page in
$pageCote = ceil($this->_currentPage / ($this->_navigationItemCount - 1)) - 1;
// Total page column
$pageCoteCount = ceil($this->_pageCount / ($this->_navigationItemCount - 1));
// Start page in paging bar
$pageStart = $pageCote * ($this->_navigationItemCount -1) + 1;
// Termination page in paging bar
$pageEnd = $pageStart + $this->_navigationItemCount - 1;
if($this->_pageCount < $pageEnd) {
$pageEnd = $this->_pageCount;
}
$navigation .= " Total: {$this->_itemCount} Article total {$this->_pageCount} Page \n ";
if($pageCote > 0) { // Home page navigation
$navigation .= '<a href="'.$this->createHref(1)
." \"="">$this->_firstPageString</a> ";
}
if($this->_currentPage != 1) { // Upper 1 Page navigation
$navigation .= '<a href="'.$this->createHref($this->_currentPage-1);
$navigation .= " \"="">$this->_previousPageString</a> ";
}else{
$navigation .= $this->_previousPageString . ' ';
}
while ($pageStart <= $pageEnd) // Construct digital navigation area
{
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) { // Under 1 Page navigation
$navigation .= ' <a href="'
. $this->createHref($this->_currentPage+1)
. " \"="">$this->_nextPageString</a> ";
}else{
$navigation .= $this->_nextPageString;
}
if ($pageCote < $pageCoteCount-1) { // No page navigation
$navigation .= '<a href="'
. $this->createHref($this->_pageCount)
. " \"="">$this->_lastPageString</a> ";
}
$navigation .= ' To <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 .= " Page </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)) { // Specify the target page
$targetUrl .= "/$this->_PageParaName/$targetPage";
} else {
$targetUrl .= "/$this->_PageParaName/";
}
return $targetUrl;
}
}
Here's a brief review of limit offset in Mysql
Assume that there are 13 pieces of data in the database table blog.
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
//9 in statement 1 means starting at row 10 of the table and returning 4 rows
//A 4 in statement 2 indicates that 4 rows are returned, and offset 9 indicates that it starts at row 10 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;