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. // The following wrappers are provided: one for each PEAR
  14. // db abstraction layer (DB, MDB and MDB2), one for
  15. // PEAR::DB_DataObject, and one for the PHP Eclipse library
  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.  * @param integer fetch mode constant
  40.  * @param array  parameters for query placeholders
  41.  * @return array with links and paged data
  42.  */
  43. function Pager_Wrapper_DB(&$db$query$pager_options = array()$disabled = false$fetchMode = DB_FETCHMODE_ASSOC$dbparams = null)
  44. {
  45.    if (!array_key_exists('totalItems'$pager_options)) {
  46.         //  be smart and try to guess the total number of records
  47.         if (stristr($query'GROUP BY'=== false{
  48.             //no GROUP BY => do a fast COUNT(*) on the rewritten query
  49.             //$queryCount = 'SELECT COUNT(*)'.stristr($query, ' FROM ');
  50.             $queryCount preg_replace('/(.|\n)*?FROM/''SELECT COUNT(*) FROM'$query1);
  51.             list($queryCountspliti('ORDER BY '$queryCount);
  52.             list($queryCountspliti('LIMIT '$queryCount);
  53.             $totalItems $db->getOne($queryCount$dbparams);
  54.             if (DB::isError($totalItems)) {
  55.                 return $totalItems;
  56.             }
  57.         else {
  58.             //GROUP BY => fetch the whole resultset and count the rows returned
  59.             $res =$db->query($query$dbparams);
  60.             if (DB::isError($res)) {
  61.                 return $res;
  62.             }
  63.             $totalItems = (int)$res->numRows();
  64.             $res->free();
  65.         }
  66.         $pager_options['totalItems'$totalItems;
  67.     }
  68.     require_once 'Pager/Pager.php';
  69.     $pager Pager::factory($pager_options);
  70.  
  71.     $page = array();
  72.     $page['totalItems'$pager_options['totalItems'];
  73.     $page['links'$pager->links;
  74.     $page['page_numbers'= array(
  75.         'current' => $pager->getCurrentPageID(),
  76.         'total'   => $pager->numPages()
  77.     );
  78.     list($page['from']$page['to']$pager->getOffsetByPageId();
  79.  
  80.     $res ($disabled)
  81.         ? $db->limitQuery($query0$totalItems$dbparams)
  82.         : $db->limitQuery($query$page['from']-1$pager_options['perPage']$dbparams);
  83.  
  84.     if (DB::isError($res)) {
  85.         return $res;
  86.     }
  87.     $page['data'= array();
  88.     while ($res->fetchInto($row$fetchMode)) {
  89.        $page['data'][$row;
  90.     }
  91.     if ($disabled{
  92.         $page['links''';
  93.         $page['page_numbers'= array(
  94.             'current' => 1,
  95.             'total'   => 1
  96.         );
  97.     }
  98.     return $page;
  99. }
  100.  
  101. /**
  102.  * @param object PEAR::MDB instance
  103.  * @param string db query
  104.  * @param array  PEAR::Pager options
  105.  * @param boolean Disable pagination (get all results)
  106.  * @param integer fetch mode constant
  107.  * @return array with links and paged data
  108.  */
  109. function Pager_Wrapper_MDB(&$db$query$pager_options = array()$disabled = false$fetchMode = MDB_FETCHMODE_ASSOC)
  110. {
  111.     if (!array_key_exists('totalItems'$pager_options)) {
  112.         //be smart and try to guess the total number of records
  113.         if (stristr($query'GROUP BY'=== false{
  114.             //no GROUP BY => do a fast COUNT(*) on the rewritten query
  115.             //$queryCount = 'SELECT COUNT(*)'.stristr($query, ' FROM ');
  116.             $queryCount preg_replace('/(.|\n)*?FROM/''SELECT COUNT(*) FROM'$query1);
  117.             list($queryCountspliti('ORDER BY '$queryCount);
  118.             list($queryCountspliti('LIMIT '$queryCount);
  119.             $totalItems $db->queryOne($queryCount);
  120.             if (MDB::isError($totalItems)) {
  121.                 return $totalItems;
  122.             }
  123.         else {
  124.             //GROUP BY => fetch the whole resultset and count the rows returned
  125.             $res $db->query($query);
  126.             if (MDB::isError($res)) {
  127.                 return $res;
  128.             }
  129.             $totalItems = (int)$db->numRows($res);
  130.             $db->freeResult($res);
  131.         }
  132.         $pager_options['totalItems'$totalItems;
  133.     }
  134.     require_once 'Pager/Pager.php';
  135.     $pager Pager::factory($pager_options);
  136.  
  137.     $page = array();
  138.     $page['totalItems'$pager_options['totalItems'];
  139.     $page['links'$pager->links;
  140.     $page['page_numbers'= array(
  141.         'current' => $pager->getCurrentPageID(),
  142.         'total'   => $pager->numPages()
  143.     );
  144.     list($page['from']$page['to']$pager->getOffsetByPageId();
  145.  
  146.     $res ($disabled)
  147.         ? $db->limitQuery($querynull0$totalItems)
  148.         : $db->limitQuery($querynull$page['from']-1$pager_options['perPage']);
  149.  
  150.     if (MDB::isError($res)) {
  151.         return $res;
  152.     }
  153.     $page['data'= array();
  154.     while ($row $db->fetchInto($res$fetchMode)) {
  155.         $page['data'][$row;
  156.     }
  157.     if ($disabled{
  158.         $page['links''';
  159.         $page['page_numbers'= array(
  160.             'current' => 1,
  161.             'total'   => 1
  162.         );
  163.     }
  164.     return $page;
  165. }
  166.  
  167. /**
  168.  * @param object PEAR::MDB2 instance
  169.  * @param string db query
  170.  * @param array  PEAR::Pager options
  171.  * @param boolean Disable pagination (get all results)
  172.  * @param integer fetch mode constant
  173.  * @return array with links and paged data
  174.  */
  175. function Pager_Wrapper_MDB2(&$db$query$pager_options = array()$disabled = false$fetchMode = MDB2_FETCHMODE_ASSOC)
  176. {
  177.     if (!array_key_exists('totalItems'$pager_options)) {
  178.         //be smart and try to guess the total number of records
  179.         if (stristr($query'GROUP BY'=== false{
  180.             //no GROUP BY => do a fast COUNT(*) on the rewritten query
  181.             //$queryCount = 'SELECT COUNT(*)'.stristr($query, ' FROM ');
  182.             $queryCount preg_replace('/(.|\n)*?FROM/''SELECT COUNT(*) FROM'$query1);
  183.             list($queryCountspliti('ORDER BY '$queryCount);
  184.             list($queryCountspliti('LIMIT '$queryCount);
  185.             $totalItems $db->queryOne($queryCount);
  186.             if (MDB2::isError($totalItems)) {
  187.                 return $totalItems;
  188.             }
  189.         else {
  190.             //GROUP BY => fetch the whole resultset and count the rows returned
  191.             $res =$db->query($query);
  192.             if (MDB2::isError($res)) {
  193.                 return $res;
  194.             }
  195.             $totalItems = (int)$res->numRows();
  196.             $res->free();
  197.         }
  198.         $pager_options['totalItems'$totalItems;
  199.     }
  200.     require_once 'Pager/Pager.php';
  201.     $pager Pager::factory($pager_options);
  202.  
  203.     $page = array();
  204.     $page['links'$pager->links;
  205.     $page['totalItems'$pager_options['totalItems'];
  206.     $page['page_numbers'= array(
  207.         'current' => $pager->getCurrentPageID(),
  208.         'total'   => $pager->numPages()
  209.     );
  210.     list($page['from']$page['to']$pager->getOffsetByPageId();
  211.     $page['limit'$page['to'$page['from'+1;
  212.     if (!$disabled{
  213.         $db->setLimit($pager_options['perPage']$page['from']-1);
  214.     }
  215.     $page['data'$db->queryAll($querynull$fetchMode);
  216.     if (MDB2::isError($page['data'])) {
  217.         return $page['data'];
  218.     }
  219.     if ($disabled{
  220.         $page['links''';
  221.         $page['page_numbers'= array(
  222.             'current' => 1,
  223.             'total'   => 1
  224.         );
  225.     }
  226.     return $page;
  227. }
  228.  
  229. /**
  230.  * @param object PEAR::DataObject instance
  231.  * @param array  PEAR::Pager options
  232.  * @param boolean Disable pagination (get all results)
  233.  * @return array with links and paged data
  234.  * @author Massimiliano Arione <garak@studenti.it>
  235.  */
  236. function Pager_Wrapper_DBDO(&$db$pager_options = array()$disabled = false)
  237. {
  238.     if (!array_key_exists('totalItems'$pager_options)) {
  239.         // be smart and try to guess the total number of records
  240.         $totalItems $db->count();
  241.         $pager_options['totalItems'$totalItems;
  242.     }
  243.     require_once 'Pager/Pager.php';
  244.     $pager Pager::factory($pager_options);
  245.  
  246.     $page = array();
  247.     $page['links'$pager->links;
  248.     $page['page_numbers'= array(
  249.         'current' => $pager->getCurrentPageID(),
  250.         'total'   => $pager->numPages()
  251.     );
  252.     list($page['from']$page['to']$pager->getOffsetByPageId();
  253.     $page['limit'$page['to'$page['from'+ 1;
  254.     if (!$disabled{
  255.         $db->limit($page['from'- 1$pager_options['perPage']);
  256.     }
  257.     $db->find();
  258.     while ($db->fetch()) {
  259.         $page['data'][$db->toArray();
  260.     }
  261.     return $page;
  262. }
  263.  
  264. /**
  265.  * @param object PHP Eclipse instance
  266.  * @param string db query
  267.  * @param array  PEAR::Pager options
  268.  * @param boolean Disable pagination (get all results)
  269.  * @return array with links and paged data
  270.  * @author Matte Edens <matte@arubanetworks.com>
  271.  * @see http://sourceforge.net/projects/eclipselib/
  272.  */
  273. function Pager_Wrapper_Eclipse(&$db$query$pager_options = array()$disabled = false)
  274. {
  275.     if (!$disabled{
  276.         require_once(ECLIPSE_ROOT . 'PagedQuery.php');
  277.         $query =new PagedQuery($db->query($query)$pager_options['perPage']);
  278.         $totalrows $query->getRowCount();
  279.         $numpages  $query->getPageCount();
  280.         $whichpage = isset($_GET[$pager_options['urlVar']]? (int)$_GET[$pager_options['urlVar']] - 1 : 0;
  281.         if ($whichpage >= $numpages{
  282.             $whichpage $numpages - 1;
  283.         }
  284.         $result $query->getPage($whichpage);
  285.     else {
  286.         $result $db->query($query);
  287.         $totalrows $result->getRowCount();
  288.         $numpages  = 1;
  289.     }
  290.     if (!$result->isSuccess()) {
  291.         return PEAR::raiseError($result->getErrorMessage());
  292.     }
  293.     if (!array_key_exists('totalItems'$pager_options)) {
  294.         $pager_options['totalItems'$totalrows;
  295.     }
  296.  
  297.     $page = array();
  298.     require_once(ECLIPSE_ROOT . 'QueryIterator.php');
  299.     for ($it =new QueryIterator($result)$it->isValid()$it->next()) {
  300.         $page['data'][=$it->getCurrent();
  301.     }
  302.     require_once 'Pager/Pager.php';
  303.     $pager Pager::factory($pager_options);
  304.  
  305.     $page['links'$pager->links;
  306.     $page['totalItems'$pager_options['totalItems'];
  307.     $page['page_numbers'= array(
  308.         'current' => $pager->getCurrentPageID(),
  309.         'total'   => $numpages
  310.     );
  311.     $page['perPageSelectBox'$pager->getperpageselectbox();
  312.     list($page['from']$page['to']$pager->getOffsetByPageId();
  313.     $page['limit'$page['to'$page['from'+ 1;
  314.     if ($disabled{
  315.         $page['links''';
  316.         $page['page_numbers'= array(
  317.             'current' => 1,
  318.             'total'   => 1
  319.         );
  320.     }
  321.     return $page;
  322. }
  323. ?>

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