home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / xamp / xampp-win32.exe / xampp / Pager_Wrapper.php < prev    next >
Encoding:
PHP Script  |  2004-10-01  |  5.6 KB  |  167 lines

  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.         $queryCount = 'SELECT COUNT(*)'.stristr($query, ' FROM ');
  45.         list($queryCount, ) = spliti('GROUP BY ', $queryCount);
  46.         list($queryCount, ) = spliti('ORDER BY ', $queryCount);
  47.         list($queryCount, ) = spliti('LIMIT ', $queryCount);
  48.         $totalItems = $db->getOne($queryCount);
  49.         if (DB::isError($totalItems)) {
  50.             return $totalItems;
  51.         }
  52.         $pager_options['totalItems'] = $totalItems;
  53.     }
  54.     require_once 'Pager/Pager.php';
  55.     $pager = Pager::factory($pager_options);
  56.  
  57.     $page = array();
  58.     $page['totalItems'] = $totalItems;
  59.     $page['links'] = $pager->links;
  60.     $page['page_numbers'] = array(
  61.         'current' => $pager->getCurrentPageID(),
  62.         'total'   => $pager->numPages()
  63.     );
  64.     list($page['from'], $page['to']) = $pager->getOffsetByPageId();
  65.  
  66.     $res = ($disabled)
  67.         ? $db->limitQuery($query, 0, $totalItems)
  68.         : $db->limitQuery($query, $page['from']-1, $pager_options['perPage']);
  69.  
  70.     if (DB::isError($res)) {
  71.         return $res;
  72.     }
  73.     $page['data'] = array();
  74.     while ($res->fetchInto($row, DB_FETCHMODE_ASSOC)) {
  75.        $page['data'][] = $row;
  76.     }
  77.     return $page;
  78. }
  79.  
  80. /**
  81.  * @param object PEAR::MDB instance
  82.  * @param string db query
  83.  * @param array  PEAR::Pager options
  84.  * @param boolean Disable pagination (get all results)
  85.  * @return array with links and paged data
  86.  */
  87. function Pager_Wrapper_MDB(&$db, $query, $pager_options = array(), $disabled = false)
  88. {
  89.     if (!array_key_exists('totalItems', $pager_options)) {
  90.         //be smart and try to guess the total number of records
  91.         $queryCount = 'SELECT COUNT(*)'.stristr($query, ' FROM ');
  92.         list($queryCount, ) = spliti('GROUP BY ', $queryCount);
  93.         list($queryCount, ) = spliti('ORDER BY ', $queryCount);
  94.         list($queryCount, ) = spliti('LIMIT ', $queryCount);
  95.         $totalItems = $db->queryOne($queryCount);
  96.         if (MDB::isError($totalItems)) {
  97.             return $totalItems;
  98.         }
  99.         $pager_options['totalItems'] = $totalItems;
  100.     }
  101.     require_once 'Pager/Pager.php';
  102.     $pager = Pager::factory($pager_options);
  103.  
  104.     $page = array();
  105.     $page['links'] = $pager->links;
  106.     $page['page_numbers'] = array(
  107.         'current' => $pager->getCurrentPageID(),
  108.         'total'   => $pager->numPages()
  109.     );
  110.     list($page['from'], $page['to']) = $pager->getOffsetByPageId();
  111.  
  112.     $res = ($disabled)
  113.         ? $db->limitQuery($query, null, 0, $totalItems)
  114.         : $db->limitQuery($query, null, $page['from']-1, $pager_options['perPage']);
  115.  
  116.     if (MDB::isError($res)) {
  117.         return $res;
  118.     }
  119.     $page['data'] = array();
  120.     while ($row = $db->fetchInto($res, MDB_FETCHMODE_ASSOC)) {
  121.         $page['data'][] = $row;
  122.     }
  123.     return $page;
  124. }
  125.  
  126. /**
  127.  * @param object PEAR::MDB2 instance
  128.  * @param string db query
  129.  * @param array  PEAR::Pager options
  130.  * @param boolean Disable pagination (get all results)
  131.  * @return array with links and paged data
  132.  */
  133. function Pager_Wrapper_MDB2(&$db, $query, $pager_options = array(), $disabled = false)
  134. {
  135.     if (!array_key_exists('totalItems', $pager_options)) {
  136.         //be smart and try to guess the total number of records
  137.         $queryCount = 'SELECT COUNT(*)'.stristr($query, ' FROM ');
  138.         list($queryCount, ) = spliti('GROUP BY ', $queryCount);
  139.         list($queryCount, ) = spliti('ORDER BY ', $queryCount);
  140.         list($queryCount, ) = spliti('LIMIT ', $queryCount);
  141.         $totalItems = $db->queryOne($queryCount);
  142.         if (MDB2::isError($totalItems)) {
  143.             return $totalItems;
  144.         }
  145.         $pager_options['totalItems'] = $totalItems;
  146.     }
  147.     require_once 'Pager/Pager.php';
  148.     $pager = Pager::factory($pager_options);
  149.  
  150.     $page = array();
  151.     $page['links'] = $pager->links;
  152.     $page['page_numbers'] = array(
  153.         'current' => $pager->getCurrentPageID(),
  154.         'total'   => $pager->numPages()
  155.     );
  156.     list($page['from'], $page['to']) = $pager->getOffsetByPageId();
  157.     $page['limit'] = $page['to'] - $page['from'] +1;
  158.     if (!$disabled) {
  159.         $db->setLimit($pager_options['perPage'], $page['from']-1);
  160.     }
  161.     $page['data'] = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
  162.     if (MDB2::isError($page['data'])) {
  163.         return $page['data'];
  164.     }
  165.     return $page;
  166. }
  167. ?>