pear的pager包简单使用

应用Mdb2包进行数据库查询,使用Pager进行分页,我想应该是最简单的表示方式.

  1. <?php
  2. require_once 'Pager/Pager.php';
  3. require_once 'MDB2.php';
  4.  
  5. //skipped the db connection code...
  6. //let's just suppose we have a valid db connection in $db.
  7.  
  8. //first, we use Pager to create the links
  9. $num_products = $db->queryOne('SELECT COUNT(*) FROM products');
  10. $pager_options = array(
  11.     'mode'       => 'Sliding',
  12.     'perPage'    => 10,
  13.     'delta'      => 2,
  14.     'totalItems' => $num_products,
  15. );
  16.  
  17. $pager = Pager::factory($pager_options);
  18.  
  19. //then we fetch the relevant records for the current page
  20. list($from, $to) = $pager->getOffsetByPageId();
  21. //set the OFFSET and LIMIT clauses for the following query
  22. $db->setLimit($pager_options['perPage'], $from - 1);
  23. $query = 'SELECT prod_name, prod_description FROM products';
  24. $products = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
  25.  
  26. //show the results
  27. echo '<ul>';
  28. foreach ($products as $product) {
  29.     echo '<li>'.$product['prod_name'].': '.$product['prod_description'].'</li>';
  30. }
  31. echo '</ul>';
  32.  
  33. //show the links
  34. echo $pager->links;
  35. ?>

RSS feed for comments on this post · TrackBack URL

发表评论

You must be logged in to post a comment.