Zend Framework 2 시작하기 - 데이터 페이지 매김 - 페이지 매김
예시 표(기사)
아이디(아이디)
사용자 아이디(uid)
제목
1
1
동티모르 대위가 대기 중입니다.
2
2
감자는 고구마를 부른다
삼
삼
고구마 받았습니다 답변 부탁드립니다
삼
1
데마시아
1단계: 데이터 조작 테이블 추가(ArticleTable)
(경로:/Module/Application/src/Application/Model/ArticleTable.php)
<?php
/**
* 分页测试,数据查询表
*
* @author Star <[email protected]>
* @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;
}
}
2단계: 현재 기사 데이터베이스 쿼리 클래스를 Module.php에 삽입
(경로:/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)
);
}
)
);
}
3단계: 컨트롤러에서 호출 정보 호출
<?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');
}
}
}
4단계: 보기에서 페이징 정보 설정
보기 파일 page.phtml 생성(경로:/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; ?>">
< 上一页
</a> |
<?php else: ?>
<span>< 上一页</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; ?>">
下一页 >
</a>
<?php else: ?>
<span>下一页 ></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; ?>
5단계: 인덱스 보기 파일에서 페이지 보기 호출
(경로:/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());
?>