来源:http://trac.seagullproject.org/wiki/Howto/DB/PagingDataobjects

分页dataobjects

概述

使用DataObject和pear分页功能替代SGL_DB方法来对结果集分页的一段代码……

在manager中:

$output->pageTitle = $this->pageTitle . ' :: Browse';
 
$categoryList = DB_DataObject::factory($this->catTableName);
 
$result = $categoryList->find();
 
// Paginate code if needed
$limit = $_SESSION['aPrefs']['resPerPage'];
 
if ($limit < $categoryList->count()) {
    require_once 'Pager/Pager.php';
    $pagerOptions = array(
        'mode'     => 'Sliding',
        'delta'    => 3,
        'perPage'  => $limit,
        'httpMethod' => 'POST',
        'importQuery' => false,
        'path'=>SGL_Output::makeUrl('list','categories','companies'),
        'totalItems'=> $categoryList->count()
    );
 
    $pager = & Pager::factory($pagerOptions);
    $output->pager = $pager;
 
    $categoryList->limit(($pager->getCurrentPageID()-1)*$limit,$limit);
    $result = $categoryList->find();
}
 
$aCategories = array();
if ($result > 0) {
    while ($categoryList->fetch()) {
        $categoryList->cat_name = $categoryList->cat_name;
        $categoryList->cat_desc = nl2br($categoryList->cat_desc);
        $aCategories[] = clone($categoryList);
    }
}
$output->results = $aCategories;

在模板中:

{if:pager}
   <flexy:include src="pearPager.html" />
{end:}

“pearPager.html”文件中的代码:

<div class="pager">
<table>
    <tr>
        <td>
            {pager.links:h}
        </td>
    </tr>
</table>
</div>

可选方案

下列这个教程将会告诉你如何使用PEAR的Structures_DataGrid和DB_DataObject对结果集进行分页:

实例代码:

<?php
class DataObject_Fruits extends DB_DataObject 
{
    var $__table = "fruits";
    var $id;
    var $name;
    var $stock;
    var $price;
}

$dataobject = new DataObject_Fruits();
$datagrid =& new Structures_DataGrid(10);
$datagrid->bind($dataobject); // the Magic
$datagrid->render();
?>
 
howto/db/pagingdataobjects.txt · 最后更改: 2010/05/30 00:21 (外部编辑)
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2