Zend\Paginator 使用实例

分页在应用开发中是必不可少的,Zend\Paginator提供的分页组件,总体感觉还算不错。数据集合分页本身的逻辑并不复杂,三个输入参数:页大小,页号,总记录数,就可以计算当前页数据已经分页导航(前一页后一页等),但是由于使用太频繁了,把这个抽象出来总是由价值的。

以下是测试例子:

<?php
$rootPath = dirname(__DIR__);
$library = $rootPath."/library";

chdir($rootPath);
if(file_exists('vendor/autoload.php')){
    $loader = include '/vendor/autoload.php';
}else{
    exit("Autoload Failed. ");
}
$loader->setPsr4("Zend\\",$library.'/zf2_psr4');

$dbAdapter = new Zend\Db\Adapter\Adapter(array(
    'hostname' => 'localhost',
    'database' => 'test',
    'username' => 'root',
    'password' => '',

    //     'driver' => 'mysqli',
    //     'charset' =>'utf8'

    'driver' => 'pdo_mysql',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    ),
));

/// 0 初始化
$query = $_GET;
unset($query['page']);

// 0 - 1 数组
// $array = array();
// for($i=0;$i<666;$i++) {
//     $array[] = array("first_name"=>"ifeeline".$i,"last_name"=>"vvvvvvv".$i);
// }
// $arrayAdapter = new Zend\Paginator\Adapter\ArrayAdapter($array);
// $paginator = new Zend\Paginator\Paginator($arrayAdapter);


// 0 - 2 DbSelect
// $select = new Zend\Db\Sql\Select();
// $select->from("datatables_demo")->columns(array("id","first_name","last_name","email"))
// ->where("age > 20");

// $dbSelectAdapter = new Zend\Paginator\Adapter\DbSelect($select, $dbAdapter);
// $paginator = new Zend\Paginator\Paginator($dbSelectAdapter);

// 0 - 3 TableGateway
$tableGateway = new Zend\Db\TableGateway\TableGateway("datatables_demo", $dbAdapter);
$tableGatewayAdapter = new Zend\Paginator\Adapter\DbTableGateway($tableGateway);
$paginator = new Zend\Paginator\Paginator($tableGatewayAdapter);


//$paginator->setDefaultScrollingStyle("All");
//$paginator->setDefaultScrollingStyle("Jumping");
$paginator->setDefaultScrollingStyle("Elastic");
//$paginator->setDefaultScrollingStyle("Sliding"); 默认

/// 1 配置
// 设置显示的页码范围
$paginator->setPageRange(10);

/// 2 修正当前页码
// 总页数
$totalPage = count($paginator);

// 获取当前页码
$page = !empty($_GET['page'])?(int)$_GET['page']:1;
if($page < 1) {
    $page = 1;
} else if($page > $totalPage) {
    $page = $totalPage;
}
$query['page'] = $page;


// 设置当前页码
$paginator->setCurrentPageNumber($page);

/// 3 取得页数据
$datas = $paginator->getCurrentItems();

/// 4 获取页码控制器
$pageCtrl = $paginator->getPages();

/// 5 页码控制器输出
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>

<table style="width:500px;">
    <tr style="backgroup-color:#ccc">
        <td style="width:150px; text-align:left;">名称</td>
        <td style="width:150px; text-align:left;">值</td>
        <td>操作</td>
    </tr>
<?php
if(!empty($datas)) {
    foreach($datas as $row) {
?>
    <tr>
        <td style="text-align:left;"><?php echo $row['first_name'];?></td>
        <td style="text-align:left;"><?php echo $row['last_name'];?></td>
        <td style="text-align:left;">编辑 更新 删除</td>
    </tr>
<?php 
    }
}
?>
</table>

<br /><br />

<?php 
if ($pageCtrl->pageCount): ?>
<div class="paginationControl">
<!-- Previous page link -->
<?php if (isset($pageCtrl->previous)): 
    $query['page'] = $pageCtrl->previous;
?>
  <a href="<?php echo "paginator.php?".http_build_query($query);?>">
    前一页
  </a> |
<?php else: ?>
  <span class="disabled">前一页</span> |
<?php endif; ?>

<!-- Numbered page links -->
<?php foreach ($pageCtrl->pagesInRange as $pageIndex): ?>
  <?php if ($pageIndex != $pageCtrl->current): 
        $query['page'] = $pageIndex;
  ?>
    <a href="<?php echo "paginator.php?".http_build_query($query);?>">
        <?php echo $pageIndex; ?>
    </a> |
  <?php else: ?>
    <?php echo $pageIndex; ?> |
  <?php endif; ?>
<?php endforeach; ?>

<!-- Next page link -->
<?php if (isset($pageCtrl->next)): 
        $query['page'] = $pageCtrl->next;
?>
  <a href="<?php echo "paginator.php?".http_build_query($query);?>">
    下一页
  </a>
<?php else: ?>
  <span class="disabled">下一页</span>
<?php endif; ?>
</div>
<?php endif;?>

<br /><br />

<?php 
if ($pageCtrl->pageCount): ?>
<div class="paginationControl">
    <?php echo $pageCtrl->firstItemNumber;?> - <?php echo $pageCtrl->lastItemNumber;?> of <?php echo $pageCtrl->totalItemCount;?>

    <!-- First page link -->
    <?php 
    if (isset($pageCtrl->previous)): 
        $query['page'] = $pageCtrl->first;
    ?>
    <a href="paginator.php?<?php echo http_build_query($query);?>">首页</a> |
    <?php else: ?>
    <span class="disabled">首页</span> |
    <?php endif; ?>

    <!-- Previous page link -->
    <?php if (isset($pageCtrl->previous)): 
        $query['page'] = $pageCtrl->previous;
    ?>
      <a href="paginator.php?<?php echo http_build_query($query);?>">
                     前一页
      </a> |
    <?php else: ?>
      <span class="disabled">前一页</span> |
    <?php endif; ?>

    <!-- Numbered page links -->
    <?php foreach ($pageCtrl->pagesInRange as $pageIndex): ?>
      <?php if ($pageIndex != $pageCtrl->current): 
            $query['page'] = $pageIndex;
      ?>
        <a href="<?php echo "paginator.php?".http_build_query($query);?>">
            <?php echo $pageIndex; ?>
        </a> |
      <?php else: ?>
        <?php echo $pageIndex; ?> |
      <?php endif; ?>
    <?php endforeach; ?>
    
    <!-- Next page link -->
    <?php if (isset($pageCtrl->next)): 
        $query['page'] = $pageCtrl->next;
    ?>
      <a href="paginator.php?<?php echo http_build_query($query);?>">
                     下一页
      </a> |
    <?php else: ?>
      <span class="disabled">下一页</span> |
    <?php endif; ?>
    
    <!-- Last page link -->
    <?php if (isset($pageCtrl->next)): 
        $query['page'] = $pageCtrl->last;
    ?>
      <a href="paginator.php?<?php echo http_build_query($query);?>">
                    末页
      </a>
    <?php else: ?>
      <span class="disabled">末页</span>
    <?php endif; ?>
</div>
<?php endif; ?>

<br /><br />

<?php if ($pageCtrl->pageCount): ?>
<select id="paginationControl" size="1">
<?php foreach ($pageCtrl->pagesInRange as $pageIndex): ?>
  <?php $selected = ($pageIndex == $pageCtrl->current) ? ' selected="selected"' : ''; ?>
  <option value=""<?php echo $selected ?>>
    <?php echo $pageIndex; ?>
  </option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</body>
</html>

paginator