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

Source for file Pager_Wrapper.php

Documentation is available at Pager_Wrapper.php

  1. <?php
  2. // CVS: $Id: Pager_Wrapper.php,v 1.16 2005/04/28 22:20:35 quipo Exp $
  3. //
  4. // Pager_Wrapper
  5. // -------------
  6. //
  7. // Ready-to-use wrappers for paging the result of a query,
  8. // when fetching the whole resultset is NOT an option.
  9. // This is a performance- and memory-savvy method
  10. // to use PEAR::Pager with a database.
  11. // With this approach, the network load can be
  12. // consistently smaller than with PEAR::DB_Pager.
  13. //
  14. // The following wrappers are provided: one for each PEAR
  15. // db abstraction layer (DB, MDB and MDB2), one for
  16. // PEAR::DB_DataObject, and one for the PHP Eclipse library
  17. //
  18. //
  19. // SAMPLE USAGE
  20. // ------------
  21. //
  22. // $query = 'SELECT this, that FROM mytable';
  23. // require_once 'Pager_Wrapper.php'; //this file
  24. // $pagerOptions = array(
  25. //     'mode'    => 'Sliding',
  26. //     'delta'   => 2,
  27. //     'perPage' => 15,
  28. // );
  29. // $paged_data = Pager_Wrapper_MDB2($db, $query, $pagerOptions);
  30. // //$paged_data['data'];  //paged data
  31. // //$paged_data['links']; //xhtml links for page navigation
  32. // //$paged_data['page_numbers']; //array('current', 'total');
  33. //
  34.  
  35. /**
  36.  * Helper method - Rewrite the query into a "SELECT COUNT(*)" query.
  37.  * @param string $sql query
  38.  * @return string rewritten query OR false if the query can't be rewritten
  39.  * @access private
  40.  */
  41. function rewriteCountQuery($sql)
  42. {
  43.     if (preg_match('/^\s*SELECT\s+DISTINCT/is'$sql|| preg_match('/\s+GROUP\s+BY\s+/is'$sql)) {
  44.         return false;
  45.     }
  46.     $queryCount preg_replace('/(.|\n)*?\s+FROM\s+/is''SELECT COUNT(*) FROM '$sql1);
  47.     list($queryCountpreg_split('/\s+ORDER\s+BY\s+/is'$queryCount);
  48.     list($queryCountpreg_split('/\s+LIMIT\s+/is'$queryCount);
  49.     return trim($queryCount);
  50. }
  51.  
  52. /**
  53.  * @param object PEAR::DB instance
  54.  * @param string db query
  55.  * @param array  PEAR::Pager options
  56.  * @param boolean Disable pagination (get all results)
  57.  * @param integer fetch mode constant
  58.  * @param mixed  parameters for query placeholders
  59.  *         If you use placeholders for table names or column names, please
  60.  *         count the # of items returned by the query and pass it as an option:
  61.  *         $pager_options['totalItems'] = count_records('some query');
  62.  * @return array with links and paged data
  63.  */
  64. function Pager_Wrapper_DB(&$db$query$pager_options = array()$disabled = false$fetchMode = DB_FETCHMODE_ASSOC$dbparams = null)
  65. {
  66.    if (!array_key_exists('totalItems'$pager_options)) {
  67.         //  be smart and try to guess the total number of records
  68.         if ($countQuery = rewriteCountQuery($query)) {
  69.             $totalItems $db->getOne($countQuery$dbparams);
  70.             if (PEAR::isError($totalItems)) {
  71.                 return $totalItems;
  72.             }
  73.         else {
  74.             $res =$db->query($query$dbparams);
  75.             if (PEAR::isError($res)) {
  76.                 return $res;
  77.             }
  78.             $totalItems = (int)$res->numRows();
  79.             $res->free();
  80.         }
  81.         $pager_options['totalItems'$totalItems;
  82.     }
  83.     require_once 'Pager/Pager.php';
  84.     $pager Pager::factory($pager_options);
  85.  
  86.     $page = array();
  87.     $page['totalItems'$pager_options['totalItems'];
  88.     $page['links'$pager->links;
  89.     $page['page_numbers'= array(
  90.         'current' => $pager->getCurrentPageID(),
  91.         'total'   => $pager->numPages()
  92.     );
  93.     list($page['from']$page['to']$pager->getOffsetByPageId();
  94.  
  95.     $res ($disabled)
  96.         ? $db->limitQuery($query0$totalItems$dbparams)
  97.         : $db->limitQuery($query$page['from']-1$pager_options['perPage']$dbparams);
  98.  
  99.     if (PEAR::isError($res)) {
  100.         return $res;
  101.     }
  102.     $page['data'= array();
  103.     while ($res->fetchInto($row$fetchMode)) {
  104.        $page['data'][$row;
  105.     }
  106.     if ($disabled{
  107.         $page['links''';
  108.         $page['page_numbers'= array(
  109.             'current' => 1,
  110.             'total'   => 1
  111.         );
  112.     }
  113.     return $page;
  114. }
  115.  
  116. /**
  117.  * @param object PEAR::MDB instance
  118.  * @param string db query
  119.  * @param array  PEAR::Pager options
  120.  * @param boolean Disable pagination (get all results)
  121.  * @param integer fetch mode constant
  122.  * @return array with links and paged data
  123.  */
  124. function Pager_Wrapper_MDB(&$db$query$pager_options = array()$disabled = false$fetchMode = MDB_FETCHMODE_ASSOC)
  125. {
  126.     if (!array_key_exists('totalItems'$pager_options)) {
  127.         //be smart and try to guess the total number of records
  128.         if ($countQuery = rewriteCountQuery($query)) {
  129.             $totalItems $db->queryOne($countQuery);
  130.             if (PEAR::isError($totalItems)) {
  131.                 return $totalItems;
  132.             }
  133.         else {
  134.             $res $db->query($query);
  135.             if (PEAR::isError($res)) {
  136.                 return $res;
  137.             }
  138.             $totalItems = (int)$db->numRows($res);
  139.             $db->freeResult($res);
  140.         }
  141.         $pager_options['totalItems'$totalItems;
  142.     }
  143.     require_once 'Pager/Pager.php';
  144.     $pager Pager::factory($pager_options);
  145.  
  146.     $page = array();
  147.     $page['totalItems'$pager_options['totalItems'];
  148.     $page['links'$pager->links;
  149.     $page['page_numbers'= array(
  150.         'current' => $pager->getCurrentPageID(),
  151.         'total'   => $pager->numPages()
  152.     );
  153.     list($page['from']$page['to']$pager->getOffsetByPageId();
  154.  
  155.     $res ($disabled)
  156.         ? $db->limitQuery($querynull0$totalItems)
  157.         : $db->limitQuery($querynull$page['from']-1$pager_options['perPage']);
  158.  
  159.     if (PEAR::isError($res)) {
  160.         return $res;
  161.     }
  162.     $page['data'= array();
  163.     while ($row $db->fetchInto($res$fetchMode)) {
  164.         $page['data'][$row;
  165.     }
  166.     if ($disabled{
  167.         $page['links''';
  168.         $page['page_numbers'= array(
  169.             'current' => 1,
  170.             'total'   => 1
  171.         );
  172.     }
  173.     return $page;
  174. }
  175.  
  176. /**
  177.  * @param object PEAR::MDB2 instance
  178.  * @param string db query
  179.  * @param array  PEAR::Pager options
  180.  * @param boolean Disable pagination (get all results)
  181.  * @param integer fetch mode constant
  182.  * @return array with links and paged data
  183.  */
  184. function Pager_Wrapper_MDB2(&$db$query$pager_options = array()$disabled = false$fetchMode = MDB2_FETCHMODE_ASSOC)
  185. {
  186.     if (!array_key_exists('totalItems'$pager_options)) {
  187.         //be smart and try to guess the total number of records
  188.         if ($countQuery = rewriteCountQuery($query)) {
  189.             $totalItems $db->queryOne($countQuery);
  190.             if (PEAR::isError($totalItems)) {
  191.                 return $totalItems;
  192.             }
  193.         else {
  194.             //GROUP BY => fetch the whole resultset and count the rows returned
  195.             $res =$db->queryCol($query);
  196.             if (PEAR::isError($res)) {
  197.                 return $res;
  198.             }
  199.             $totalItems count($res);
  200.         }
  201.         $pager_options['totalItems'$totalItems;
  202.     }
  203.     require_once 'Pager/Pager.php';
  204.     $pager Pager::factory($pager_options);
  205.  
  206.     $page = array();
  207.     $page['links'$pager->links;
  208.     $page['totalItems'$pager_options['totalItems'];
  209.     $page['page_numbers'= array(
  210.         'current' => $pager->getCurrentPageID(),
  211.         'total'   => $pager->numPages()
  212.     );
  213.     list($page['from']$page['to']$pager->getOffsetByPageId();
  214.     $page['limit'$page['to'$page['from'+1;
  215.     if (!$disabled{
  216.         $db->setLimit($pager_options['perPage']$page['from']-1);
  217.     }
  218.     $page['data'$db->queryAll($querynull$fetchMode);
  219.     if (PEAR::isError($page['data'])) {
  220.         return $page['data'];
  221.     }
  222.     if ($disabled{
  223.         $page['links''';
  224.         $page['page_numbers'= array(
  225.             'current' => 1,
  226.             'total'   => 1
  227.         );
  228.     }
  229.     return $page;
  230. }
  231.  
  232. /**
  233.  * @param object PEAR::DataObject instance
  234.  * @param array  PEAR::Pager options
  235.  * @param boolean Disable pagination (get all results)
  236.  * @return array with links and paged data
  237.  * @author Massimiliano Arione <garak@studenti.it>
  238.  */
  239. function Pager_Wrapper_DBDO(&$db$pager_options = array()$disabled = false)
  240. {
  241.     if (!array_key_exists('totalItems'$pager_options)) {
  242.         $totalItems $db->count();
  243.         $pager_options['totalItems'$totalItems;
  244.     }
  245.     require_once 'Pager/Pager.php';
  246.     $pager Pager::factory($pager_options);
  247.  
  248.     $page = array();
  249.     $page['links'$pager->links;
  250.     $page['page_numbers'= array(
  251.         'current' => $pager->getCurrentPageID(),
  252.         'total'   => $pager->numPages()
  253.     );
  254.     list($page['from']$page['to']$pager->getOffsetByPageId();
  255.     $page['limit'$page['to'$page['from'+ 1;
  256.     if (!$disabled{
  257.         $db->limit($page['from'- 1$pager_options['perPage']);
  258.     }
  259.     $db->find();
  260.     while ($db->fetch()) {
  261.         $page['data'][$db->toArray();
  262.     }
  263.     return $page;
  264. }
  265.  
  266. /**
  267.  * @param object PHP Eclipse instance
  268.  * @param string db query
  269.  * @param array  PEAR::Pager options
  270.  * @param boolean Disable pagination (get all results)
  271.  * @return array with links and paged data
  272.  * @author Matte Edens <matte@arubanetworks.com>
  273.  * @see http://sourceforge.net/projects/eclipselib/
  274.  */
  275. function Pager_Wrapper_Eclipse(&$db$query$pager_options = array()$disabled = false)
  276. {
  277.     if (!$disabled{
  278.         require_once(ECLIPSE_ROOT . 'PagedQuery.php');
  279.         $query =new PagedQuery($db->query($query)$pager_options['perPage']);
  280.         $totalrows $query->getRowCount();
  281.         $numpages  $query->getPageCount();
  282.         $whichpage = isset($_GET[$pager_options['urlVar']]? (int)$_GET[$pager_options['urlVar']] - 1 : 0;
  283.         if ($whichpage >= $numpages{
  284.             $whichpage $numpages - 1;
  285.         }
  286.         $result $query->getPage($whichpage);
  287.     else {
  288.         $result    $db->query($query);
  289.         $totalrows $result->getRowCount();
  290.         $numpages  = 1;
  291.     }
  292.     if (!$result->isSuccess()) {
  293.         return PEAR::raiseError($result->getErrorMessage());
  294.     }
  295.     if (!array_key_exists('totalItems'$pager_options)) {
  296.         $pager_options['totalItems'$totalrows;
  297.     }
  298.  
  299.     $page = array();
  300.     require_once(ECLIPSE_ROOT . 'QueryIterator.php');
  301.     for ($it =new QueryIterator($result)$it->isValid()$it->next()) {
  302.         $page['data'][=$it->getCurrent();
  303.     }
  304.     require_once 'Pager/Pager.php';
  305.     $pager Pager::factory($pager_options);
  306.  
  307.     $page['links']        $pager->links;
  308.     $page['totalItems']   $pager_options['totalItems'];
  309.     $page['page_numbers'= array(
  310.         'current' => $pager->getCurrentPageID(),
  311.         'total'   => $numpages
  312.     );
  313.     $page['perPageSelectBox'$pager->getperpageselectbox();
  314.     list($page['from']$page['to']$pager->getOffsetByPageId();
  315.     $page['limit'$page['to'$page['from'+1;
  316.     if ($disabled{
  317.         $page['links''';
  318.         $page['page_numbers'= array(
  319.             'current' => 1,
  320.             'total'   => 1
  321.         );
  322.     }
  323.     return $page;
  324. }
  325. ?>

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