pear的pager包简单使用
应用Mdb2包进行数据库查询,使用Pager进行分页,我想应该是最简单的表示方式.
- <?php
- require_once 'Pager/Pager.php';
- require_once 'MDB2.php';
- //skipped the db connection code...
- //let's just suppose we have a valid db connection in $db.
- //first, we use Pager to create the links
- $num_products = $db->queryOne('SELECT COUNT(*) FROM products');
- $pager_options = array(
- 'mode' => 'Sliding',
- 'perPage' => 10,
- 'delta' => 2,
- 'totalItems' => $num_products,
- );
- $pager = Pager::factory($pager_options);
- //then we fetch the relevant records for the current page
- list($from, $to) = $pager->getOffsetByPageId();
- //set the OFFSET and LIMIT clauses for the following query
- $db->setLimit($pager_options['perPage'], $from - 1);
- $query = 'SELECT prod_name, prod_description FROM products';
- $products = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
- //show the results
- echo '<ul>';
- foreach ($products as $product) {
- echo '<li>'.$product['prod_name'].': '.$product['prod_description'].'</li>';
- }
- echo '</ul>';
- //show the links
- echo $pager->links;
- ?>