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

Documentation generated on Mon, 11 Mar 2019 14:23:41 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.