Pager
[ class tree: Pager ] [ index: Pager ] [ all elements ]

Source for file Pager_Wrapper.php

Documentation is available at Pager_Wrapper.php

  1. <?php
  2. //
  3. // Pager_Wrapper
  4. // -------------
  5. //
  6. // Ready-to-use wrappers for paging the result of a query,
  7. // when fetching the whole resultset is NOT an option.
  8. // This is a performance- and memory-savvy method
  9. // to use PEAR::Pager with a database.
  10. // With this approach, the network load can be
  11. // consistently smaller than with PEAR::DB_Pager.
  12. //
  13. // Three wrappers are provided, one for each
  14. // PEAR db abstraction layer (DB, MDB and MDB2).
  15. //
  16. //
  17. // SAMPLE USAGE
  18. // ------------
  19. //
  20. // $query = 'SELECT this, that FROM mytable';
  21. // require_once 'Pager_Wrapper.php'; //this file
  22. // $pagerOptions = array(
  23. //     'mode'     => 'Sliding',
  24. //     'delta'    => 2,
  25. //     'perPage'  => 15,
  26. // );
  27. // $paged_data = Pager_Wrapper_MDB2($db, $query, $pagerOptions);
  28. // //$paged_data['data'];   //paged data
  29. // //$paged_data['links'];   //xhtml links for page navigation
  30. // //$paged_data['page_numbers'];   //array('current', 'total');
  31. //
  32.  
  33. /**
  34.  * @param object PEAR::DB instance
  35.  * @param string db query
  36.  * @param array  PEAR::Pager options
  37.  * @param boolean Disable pagination (get all results)
  38.  * @return array with links and paged data
  39.  */
  40. function Pager_Wrapper_DB(&$db$query$pager_options = array()$disabled = false)
  41. {
  42.    if (!array_key_exists('totalItems'$pager_options)) {
  43.         //  be smart and try to guess the total number of records
  44.         if (stristr($query'GROUP BY'=== false{
  45.             //no GROUP BY => do a fast COUNT(*) on the rewritten query
  46.             $queryCount 'SELECT COUNT(*)'.stristr($query' FROM ');
  47.             list($queryCountspliti('ORDER BY '$queryCount);
  48.             list($queryCountspliti('LIMIT '$queryCount);
  49.             $totalItems $db->getOne($queryCount);
  50.             if (DB::isError($totalItems)) {
  51.                 return $totalItems;
  52.             }
  53.         else {
  54.             //GROUP BY => fetch the whole resultset and count the rows returned
  55.             $res =$db->query($query);
  56.             if (DB::isError($res)) {
  57.                 return $res;
  58.             }
  59.             $totalItems = (int)$res->numRows();
  60.             $res->free();
  61.         }
  62.         $pager_options['totalItems'$totalItems;
  63.     }
  64.     require_once 'Pager/Pager.php';
  65.     $pager Pager::factory($pager_options);
  66.  
  67.     $page = array();
  68.     $page['totalItems'$pager_options['totalItems'];
  69.     $page['links'$pager->links;
  70.     $page['page_numbers'= array(
  71.         'current' => $pager->getCurrentPageID(),
  72.         'total'   => $pager->numPages()
  73.     );
  74.     list($page['from']$page['to']$pager->getOffsetByPageId();
  75.  
  76.     $res ($disabled)
  77.         ? $db->limitQuery($query0$totalItems)
  78.         : $db->limitQuery($query$page['from']-1$pager_options['perPage']);
  79.  
  80.     if (DB::isError($res)) {
  81.         return $res;
  82.     }
  83.     $page['data'= array();
  84.     while ($res->fetchInto($rowDB_FETCHMODE_ASSOC)) {
  85.        $page['data'][$row;
  86.     }
  87.     if ($disabled{
  88.         $page['links''';
  89.         $page['page_numbers'= array(
  90.             'current' => 1,
  91.             'total'   => 1
  92.         );
  93.     }
  94.     return $page;
  95. }
  96.  
  97. /**
  98.  * @param object PEAR::MDB instance
  99.  * @param string db query
  100.  * @param array  PEAR::Pager options
  101.  * @param boolean Disable pagination (get all results)
  102.  * @return array with links and paged data
  103.  */
  104. function Pager_Wrapper_MDB(&$db$query$pager_options = array()$disabled = false)
  105. {
  106.     if (!array_key_exists('totalItems'$pager_options)) {
  107.         //be smart and try to guess the total number of records
  108.         if (stristr($query'GROUP BY'=== false{
  109.             //no GROUP BY => do a fast COUNT(*) on the rewritten query
  110.             $queryCount 'SELECT COUNT(*)'.stristr($query' FROM ');
  111.             list($queryCountspliti('ORDER BY '$queryCount);
  112.             list($queryCountspliti('LIMIT '$queryCount);
  113.             $totalItems $db->queryOne($queryCount);
  114.             if (MDB::isError($totalItems)) {
  115.                 return $totalItems;
  116.             }
  117.         else {
  118.             //GROUP BY => fetch the whole resultset and count the rows returned
  119.             $res $db->query($query);
  120.             if (MDB::isError($res)) {
  121.                 return $res;
  122.             }
  123.             $totalItems = (int)$db->numRows($res);
  124.             $db->freeResult($res);
  125.         }
  126.         $pager_options['totalItems'$totalItems;
  127.     }
  128.     require_once 'Pager/Pager.php';
  129.     $pager Pager::factory($pager_options);
  130.  
  131.     $page = array();
  132.     $page['totalItems'$pager_options['totalItems'];
  133.     $page['links'$pager->links;
  134.     $page['page_numbers'= array(
  135.         'current' => $pager->getCurrentPageID(),
  136.         'total'   => $pager->numPages()
  137.     );
  138.     list($page['from']$page['to']$pager->getOffsetByPageId();
  139.  
  140.     $res ($disabled)
  141.         ? $db->limitQuery($querynull0$totalItems)
  142.         : $db->limitQuery($querynull$page['from']-1$pager_options['perPage']);
  143.  
  144.     if (MDB::isError($res)) {
  145.         return $res;
  146.     }
  147.     $page['data'= array();
  148.     while ($row $db->fetchInto($resMDB_FETCHMODE_ASSOC)) {
  149.         $page['data'][$row;
  150.     }
  151.     if ($disabled{
  152.         $page['links''';
  153.         $page['page_numbers'= array(
  154.             'current' => 1,
  155.             'total'   => 1
  156.         );
  157.     }
  158.     return $page;
  159. }
  160.  
  161. /**
  162.  * @param object PEAR::MDB2 instance
  163.  * @param string db query
  164.  * @param array  PEAR::Pager options
  165.  * @param boolean Disable pagination (get all results)
  166.  * @return array with links and paged data
  167.  */
  168. function Pager_Wrapper_MDB2(&$db$query$pager_options = array()$disabled = false)
  169. {
  170.     if (!array_key_exists('totalItems'$pager_options)) {
  171.         //be smart and try to guess the total number of records
  172.         if (stristr($query'GROUP BY'=== false{
  173.             //no GROUP BY => do a fast COUNT(*) on the rewritten query
  174.             $queryCount 'SELECT COUNT(*)'.stristr($query' FROM ');
  175.             list($queryCountspliti('ORDER BY '$queryCount);
  176.             list($queryCountspliti('LIMIT '$queryCount);
  177.             $totalItems $db->queryOne($queryCount);
  178.             if (MDB2::isError($totalItems)) {
  179.                 return $totalItems;
  180.             }
  181.         else {
  182.             //GROUP BY => fetch the whole resultset and count the rows returned
  183.             $res =$db->query($query);
  184.             if (MDB2::isError($res)) {
  185.                 return $res;
  186.             }
  187.             $totalItems = (int)$res->numRows();
  188.             $res->free();
  189.         }
  190.         $pager_options['totalItems'$totalItems;
  191.     }
  192.     require_once 'Pager/Pager.php';
  193.     $pager Pager::factory($pager_options);
  194.  
  195.     $page = array();
  196.     $page['links'$pager->links;
  197.     $page['totalItems'$pager_options['totalItems'];
  198.     $page['page_numbers'= array(
  199.         'current' => $pager->getCurrentPageID(),
  200.         'total'   => $pager->numPages()
  201.     );
  202.     list($page['from']$page['to']$pager->getOffsetByPageId();
  203.     $page['limit'$page['to'$page['from'+1;
  204.     if (!$disabled{
  205.         $db->setLimit($pager_options['perPage']$page['from']-1);
  206.     }
  207.     $page['data'$db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  208.     if (MDB2::isError($page['data'])) {
  209.         return $page['data'];
  210.     }
  211.     if ($disabled{
  212.         $page['links''';
  213.         $page['page_numbers'= array(
  214.             'current' => 1,
  215.             'total'   => 1
  216.         );
  217.     }
  218.     return $page;
  219. }
  220. ?>

Documentation generated on Mon, 11 Mar 2019 13:58:53 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.