Home Backend Development PHP Tutorial Very easy to use Zend Framework paging class_PHP tutorial

Very easy to use Zend Framework paging class_PHP tutorial

Jul 13, 2016 am 10:26 AM
framework zend

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();

$ This-& gt; _pageCount = Ceil ($ itemcount/$ pagesize); // Total page
$page = $this->_front->getRequest()->getParam($this->_PageParaName);
                                   
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;
}

Public function getNavigation()
{
          $navigation = '
';
                                                        //What column is the current page in?
$pageCote = ceil($this->_currentPage / ($this->_navigationItemCount - 1)) - 1;
                 //Total paging column
$pageCoteCount = ceil($this->_pageCount / ($this->_navigationItemCount - 1));
               //Start page in the pagination column
$pageStart = $pageCote * ($this->_navigationItemCount -1) + 1;
                                                                                                                                                                                          but but the                                             $pageEnd = $pageStart + $this->_navigationItemCount - 1;                                                         If($this->_pageCount < $pageEnd) {
$pageEnd = $this->_pageCount;
        }
                                                                     $navigation .= "Total: {$this->_itemCount} items Total {$this->_pageCount} pages n ";
                                                                     if($pageCote > 0) {                                                                                                                                                                                                                                       $navigation .= '$this->_firstPageString ";
        }
If($this->_currentPage != 1) { //Previous page navigation
                $navigation .= '$this->_previousPageString ";
          }else{
                 $navigation .= $this->_previousPageString . ' ';
        }
                                                              while ($pageStart <= $pageEnd)                                                                                                                                                                                                                                                            {
If ($pageStart == $this->_currentPage) {
                     $navigation .= "$pageStart" . $this->_splitString;
               } else {
                     $navigation .= ' . $this->createHref($this->_currentPage+1)
                           . " \"="">$this->_nextPageString
";
        }else{
            $navigation .= $this->_nextPageString;
        }
        
        if ($pageCote < $pageCoteCount-1) {               //未页导航
            $navigation .= '$this->_lastPageString ";
        }
 
        $navigation .= ' 到 ';
        $navigation .= " 页
";
        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 target page
              $targetUrl .= "/$this->_PageParaName/$targetPage";
         } else {
                $targetUrl .= "/$this->_PageParaName/";
       }
          return $targetUrl;
}
}

Here is a brief review of limit offset in Mysql

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;

www.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...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix May 05, 2023 pm 04:01 PM

.NET Framework 4 is required by developers and end users to run the latest versions of applications on Windows. However, while downloading and installing .NET Framework 4, many users complained that the installer stopped midway, displaying the following error message - " .NET Framework 4 has not been installed because Download failed with error code 0x800c0006 ". If you are also experiencing it while installing .NETFramework4 on your device then you are at the right place

How to identify Windows upgrade issues using SetupDiag on Windows 11/10 How to identify Windows upgrade issues using SetupDiag on Windows 11/10 Apr 17, 2023 am 10:07 AM

Whenever your Windows 11 or Windows 10 PC has an upgrade or update issue, you will usually see an error code indicating the actual reason behind the failure. However, sometimes confusion can arise when an upgrade or update fails without an error code being displayed. With handy error codes, you know exactly where the problem is so you can try to fix it. But since no error code appears, it becomes challenging to identify the issue and resolve it. This will take up a lot of your time to simply find out the reason behind the error. In this case, you can try using a dedicated tool called SetupDiag provided by Microsoft that helps you easily identify the real reason behind the error.

SCNotification has stopped working [5 steps to fix it] SCNotification has stopped working [5 steps to fix it] May 17, 2023 pm 09:35 PM

As a Windows user, you are likely to encounter SCNotification has stopped working error every time you start your computer. SCNotification.exe is a Microsoft system notification file that crashes every time you start your PC due to permission errors and network failures. This error is also known by its problematic event name. So you might not see this as SCNotification having stopped working, but as bug clr20r3. In this article, we will explore all the steps you need to take to fix SCNotification has stopped working so that it doesn’t bother you again. What is SCNotification.e

Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022 Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022 Apr 17, 2023 pm 02:25 PM

Microsoft Windows users who have installed Microsoft.NET version 4.5.2, 4.6, or 4.6.1 must install a newer version of the Microsoft Framework if they want Microsoft to support the framework through future product updates. According to Microsoft, all three frameworks will cease support on April 26, 2022. After the support date ends, the product will not receive "security fixes or technical support." Most home devices are kept up to date through Windows updates. These devices already have newer versions of frameworks installed, such as .NET Framework 4.8. Devices that are not updating automatically may

KB5012643 for Windows 11 breaks .NET Framework 3.5 apps KB5012643 for Windows 11 breaks .NET Framework 3.5 apps May 09, 2023 pm 01:07 PM

It's been a week since we talked about the new safe mode bug affecting users who installed KB5012643 for Windows 11. This pesky issue didn't appear on the list of known issues Microsoft posted on launch day, thus catching everyone by surprise. Well, just when you thought things couldn't get any worse, Microsoft drops another bomb for users who have installed this cumulative update. Windows 11 Build 22000.652 causes more problems So the tech company is warning Windows 11 users that they may experience problems launching and using some .NET Framework 3.5 applications. Sound familiar? But please don't be surprised

How to use ACL (Access Control List) for permission control in Zend Framework How to use ACL (Access Control List) for permission control in Zend Framework Jul 29, 2023 am 09:24 AM

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

PHP Implementation Framework: Zend Framework Getting Started Tutorial PHP Implementation Framework: Zend Framework Getting Started Tutorial Jun 19, 2023 am 08:09 AM

PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

Cooler Master and Framework launch innovative mini case kit, compatible with laptop motherboards Cooler Master and Framework launch innovative mini case kit, compatible with laptop motherboards Dec 15, 2023 pm 05:35 PM

According to news on December 9, Cooler Master recently demonstrated a mini chassis kit in cooperation with notebook modular solution provider Framework at a demonstration event at the Taipei Compute Show. The unique thing about this kit is that it can be compatible with and Install the motherboard from the framework notebook. Currently, this product has begun to be sold on the market, priced at 39 US dollars, which is equivalent to approximately 279 yuan at the current exchange rate. The model number of this chassis kit is named "frameWORKMAINBOARDCASE". In terms of design, it embodies the ultimate compactness and practicality, measuring only 297x133x15 mm. Its original design is to be able to seamlessly connect to framework notebooks

See all articles