Zend Framework 2 入门-数据分页-pagination

本文详细介绍了如何使用ZF2框架实现分页功能,包括数据操作表的创建、Module配置、Controller调用分页信息及视图展示分页信息等步骤。通过实例展示了如何在实际应用中应用分页功能,提高用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文介绍ZF2自带的分页

示例表(article)

ID(id)用户id(uid)标题(title)
11Timor队长正在待命
22土豆呼叫地瓜
33地瓜收到请回答
31德玛西亚

Step 1:添加数据操作表(ArticleTable)

(path: /Module/Application/src/Application/Model/ArticleTable.php)

 <?php
/**
 * 分页测试,数据查询表
 * 
 * @author Star <wmistar@gmail.com>
 * @license http://mushroot.com
 * @version: 1.0
 *
 */
namespace Application\Model;

use Zend\Db\TableGateway\TableGateway; 
class ArticleTable
{
    private $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    /**
     * 文章分页信息
     * 
     * @return \Zend\Paginator\Paginator
     */
    public function getAllArticle()
    {
        $select = $this->tableGateway->select();
        $select->columns(array('id','uid','title'));

        $adapter   = new \Zend\Paginator\Adapter\DbSelect($select, $this->sql);
        $paginator = new \Zend\Paginator\Paginator($adapter);

        return $paginator;
    }
}

Step 2:在Module.php中注入当前文章数据库查询类

(path: /Module/Application/Module.php)

use Application\Model\ArticleTable;

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Table\Application\Article' => function ($sm) {
                 $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                 return new ArticleTable(
                     new TableGateway('ys_city', $dbAdapter)
                 );
            }
        )
    );
}

Step 3:在Controller中调用分页信息

<?php
/**
 * 
 * @author Star 
 * @copyright © 2013-2113 yousha.me
 * @license http://www.yousha.me
 * @version: file_name 1.0 2013-7-26 下午7:53:14 Star
 *
 */
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController
{
    private $articleTable;

    public function indexAction()
    {
        $articleTable = $this->getArticleTable(); 
        $paginator = $$paginator->getAllArticle();
        //设置当前页码
        $paginator->setCurrentPageNumber($this->getRequest()->getQuery('page'));
        //设置每页数量(在这里设置2条数据)
        $paginator->setDefaultItemCountPerPage(2);

        return array(
            'page' -> $paginator,
        );
    }

    /**
     * 返回文章信息查询表表
     * 
     * @return \Application\Model\ArticleTable
     */
    private function getArticleTable()
    {
        if (!$this->articleTable) {
            $sm = $this->getServiceLocator();
	    $this->userTable = $sm->get('Table\Application\Article');
        }
    }
}

Step 4:在视图中设置分页信息

建立视图文件page.phtml
(path: /module/Application/views/application/index/page.phtml)

<?php if ($this->pageCount): ?>
<div>

<!-- First page link -->
<?php if (isset($this->previous)): ?>
<a href="?page=<?php echo $this->first; ?>">
首页
</a> |
<?php else: ?>
<span>首页</span> |
<?php endif; ?>

<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="?page=<?php echo $this->previous; ?>">
&lt; 上一页
</a> |
<?php else: ?>
<span>&lt; 上一页</span> |
<?php endif; ?>

<!-- Numbered page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="?page=<?php echo $page; ?>">
<?php echo $page; ?>
</a> |
<?php else: ?>
<a style="color: blue;">
<?php echo $page; ?>
</a> |
<?php endif; ?>
<?php endforeach; ?>

<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="?page=<?php echo $this->next; ?>">
下一页 &gt;
</a>
<?php else: ?>
<span>下一页 &gt;</span>
<?php endif; ?>

<!-- Last page link -->
<?php if (isset($this->next)): ?>
<a href="?page=<?php echo $this->last; ?>">
末页
</a>
<?php else: ?>
<span>末页</span>
<?php endif; ?>

</div>
<?php endif; ?>

Step 5:在Index视图文件中调用page视图

(path: /module/Application/views/application/index/index.phtml)

<?php if (0 < count($this->page)) : ?>
<table>
<tr>
    <td>ID</td>
    <td>Title</td>
</tr>
<?php foreach ($this->page as $item) : ?>
<tr>
    <td><?php echo $item->id;?></td>
    <td><?php echo $otem->title;?></td>
</tr>
<?php endforeach;?>
</table>
<?php endif; ?>
<?php 
 echo $this->paginationControl($this->page,    
            'Sliding', 'page/list',array());
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值