EllisLab text mark
Advanced Search
     
Paged Result Library
Posted: 25 September 2007 11:59 PM   [ Ignore ]
Avatar
Joined: 2007-06-10
2919 posts

This might be of some use to people having difficulty with CI pagination as it doesn’t use the db limit or offset features.

Simply pass in a reference to your full resultset, pagesize and required pagenumber.
Use the fetchResult() function retrieve the current page rows one at a time.

I adapted this from a mysqlPagedResult class I’ve been using for a few years, don’t remember the source.

<?php

class PagedResult
{
  
var $resultset = array();
  var 
$numrows;
  var 
$numPages;
  var 
$pageSize;
  var 
$page;
  var 
$row;
  var 
$ptr;
  
  function 
PagedResult($resultset=array(), $pageSize=1$resultpage=1)
  
{
    
if (!$resultset) return NULL;
    
    
$this->resultset $resultset;
    
$this->numrows count($this->resultset->result());
    
$this->pageSize $pageSize;
    
$this->numPages $this->getNumPages();
   
    if ((int)
$resultpage <= 0$resultpage 1;
    if (
$resultpage $this->numPages$resultpage $this->numPages;
    
    
$this->setPageNum($resultpage);
  
}
  
  
function getNumPages()
  
{
    
if (!$this->resultset) return FALSE;
    return 
ceil($this->numrows / (float)$this->pageSize);
  
}
  
  
function setPageNum($pageNum)
  
{
    
if ($pageNum $this->numPages or $pageNum <= 0) return FALSE;
    
$this->page $pageNum;
    
$this->row 0;
    
$this->ptr = ($pageNum-1) * $this->pageSize;
  
}
  
  
function getPageNum()
  
{
    
return $this->page;
  
}
  
  
function isLastPage()
  
{
    
return ($this->page >= $this->numPages);
  
}
  
  
function isFirstPage()
  
{
    
return ($this->page <= 1);
  
}
  
  
function fetchResult($type='object')
  
{
    
if (!$this->resultset) return FALSE;
    if (
$this->row >= $this->pageSize) return FALSE;
    if (
$this->ptr >= $this->numrows) return FALSE;
    
    
$result = ($type == 'object') ? 
        
$this->resultset->row($this->ptr) : $this->resultset->row_array($this->ptr);
    
    
$this->ptr++;
    
$this->row++;
    return 
$result;
  
}
  
  
function getPagedNav($css_class=''$url=''$queryvars '')
  
{    
    
if ($queryvars$queryvars '/'.$queryvars;
    
    if (!
$this->isFirstPage())
    
{
      $nav 
.= '<a class="'.$css_class.'" href="'.$url.
              (
$this->getPageNum()-1).$queryvars.'"><< Previous page</a>&nbsp;&nbsp;';
    
}
    
if ($this->numPages 1)
      for (
$i=1$i <= $this->numPages$i++)
      
{
        
if ($i==$this->page)
          
$nav .= '&nbsp;'.$i.'&nbsp;';
        else
          
$nav .= '&nbsp;<a class="'.$css_class.'" href="'.$url.$i.$queryvars.'">'.$i.'</a>&nbsp;';
      
}
    
if (!$this->isLastPage())
    
{
      $nav 
.= '&nbsp;&nbsp;<a class="'.$css_class.'" href="'.$url.
              (
$this->getPageNum()+1).$queryvars.'">Next page >></a>';
    
}
    
return $nav;
  
}
}
?> 

Also you can use the getPagedNav() function to render your page links and add additional URI segments into the page request, which is not available in CI pagination. (I don’t think)

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 27 September 2007 02:49 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-05-10
10 posts

i developed my own helper for paging results. 

the urls take the following format:

/controller/field(s)alias/sort_direction/page_number/results_per_page

e.g.

/listing/artist/1/5/10

lists artists in reverse order (0 is default, ascending), page 5, 10 results per page


v

 
Posted: 28 September 2007 01:33 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2006-08-22
149 posts
vijinho - 27 September 2007 06:49 PM

i developed my own helper for paging results. 

the urls take the following format:

/controller/field(s)alias/sort_direction/page_number/results_per_page

e.g.

/listing/artist/1/5/10

lists artists in reverse order (0 is default, ascending), page 5, 10 results per page


v

If you believe your code is superior and may be usefull to someone else, then why not share it?

 Signature 

Don’t argue with an idiot, people watching may not be able to tell the difference.


marcoss
http://defmay.com
http://fenix.st

 
Posted: 28 September 2007 05:12 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2007-05-10
10 posts

I would, but I don’t feel the code quality is of a high enough standard yet. 

However below is my helper, if anyone finds it useful and is prepared to help improve it.

I’ve commented it roughly, but the baseurl is the controller url, and the url path is that url appended with the options for paging I described in my previous post.

It will return something that looks like this which the controller then handles and passes to the view.

It should be self explanatory really, but if something doesn’t make sense let me know.  Also it requires a CI config value ‘results_per_page’ to find a default.


Array
(
  [results] => 247
  [pages] => 25
  [page] => 6
  [previous] => 5
  [next] => 7
  [from] => 50
  [to] => 59
  [offset] => 10
  [baseurl] => http://home.example.com/index.php/admin/countries/listing
  => [url=http://home.example.com/index.php/admin/countries/listing/country/1]http://home.example.com/index.php/admin/countries/listing/country/1
  [links] => Array
      (
        [1] => http://home.example.com/index.php/admin/countries/listing/country/1/1/10
        [2] => http://home.example.com/index.php/admin/countries/listing/country/1/2/10
        [3] => http://home.example.com/index.php/admin/countries/listing/country/1/3/10
        [4] => http://home.example.com/index.php/admin/countries/listing/country/1/4/10
        [5] => http://home.example.com/index.php/admin/countries/listing/country/1/5/10
        [6] => http://home.example.com/index.php/admin/countries/listing/country/1/6/10
        [7] => http://home.example.com/index.php/admin/countries/listing/country/1/7/10
        [8] => http://home.example.com/index.php/admin/countries/listing/country/1/8/10
        [9] => http://home.example.com/index.php/admin/countries/listing/country/1/9/10
        [10] => http://home.example.com/index.php/admin/countries/listing/country/1/10/10
      )

)


<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
/**
* Fail Pager Helpers
*
* @package         Fail
* @subpackage Helpers
* @category   Helpers
* @author         Vijay Mahrra <vijay@yoyo.org>
* @link           http://www.designbyfail.com/
*/

//————————————————————————————————————

/**
* Paging Helper
*
* Takes a number, amount per page and page number
* Returns an array with:
*    results - number of results
*    pages - the total number of pages in the result set
*    page - the current page to get results for
*    next - the next page (or null if last)
*    previous - the previous page (or null if first)
*    from - the position in the results for the page to display
*    to - the results position of the last result to display on the page
*    offset - the offset (to - from)
*
* @access     public
* @param   rows   integer   number of rows
* @param   perpage integer   rows to display per page
* @param   page   integer   current page number
* @param   url   string   the base url for the page
* @return     string
*/
function fail_pager($rows, &$perpage, &$page, $baseurl, $field, $sortdir = 0, $links = 20)
{
      $CI =& get_instance();
      $max_rows = $CI->config->item(‘limit_max_rows’);

      // get the amount we are viewing per page
      if (empty($perpage) || $perpage > $max_rows) {
        $results_per_page = $CI->config->item(‘results_per_page’);
        $perpage = $results_per_page;
      }

      // establish page we are currently on and maximum pages
      $lastpage = round($rows / $perpage);
      if ($page > $lastpage) {
        $page = $lastpage;
      } else if ($page < 1) {
        $page = 1;
      }
      $nextpage = $page + 1;
      if ($nextpage > $lastpage) {
        $nextpage = null;
      }
      $prevpage = $page - 1;
      if ($prevpage < 1) {
        $prevpage = null;
      }

      // page starts and ends at these rows in the db
      $from = ($page * $perpage) - $perpage;
      if ($from <= 0) $from = 0;
      $to = $from + $perpage - 1;

      $pager = array(
        ‘results’ => $rows,
        ‘pages’ => $lastpage,
        ‘page’ => $page,
        ‘previous’ => $prevpage,
        ‘next’ => $nextpage,
        ‘from’ => $from,
        ‘to’ => $to,
        ‘offset’ => $perpage,
        ‘baseurl’ => $baseurl,
        ‘urlpath’ => join(’/’, array($baseurl, $field, $sortdir))
      );

      $pages = array();
      $half = $links / 2;
      $c = $half;
      for ($i = $pager[‘page’]; ($i > 0 && $i > $pager[‘page’] - $half - 1); $i—) {
        $c—;
        $pages[$i] = $i;
      }
      for ($i = $pager[‘page’]; $i < ($c + $half + 1 + $pager[‘page’]) && $i < $pager[‘pages’]; $i++)
      {
        $pages[$i] = $i;
      }
      sort($pages);
      if (count($pages) > 0) {
        foreach ($pages as $k => $v) {
          $linksarray[$v] = $pager[‘urlpath’] . ‘/’ . $v . ‘/’ . $pager[‘offset’];
        }
      } else {
        for ($i = $from; $i < $to; $i++) {
          $linksarray[$i - $from] = $pager[‘urlpath’] . ‘/’ . $i - $from . ‘/’ . $pager[‘offset’];
        }
      }
      $pager[‘links’] = $linksarray;
      return $pager;
}

//————————————————————————————————————
?>

jay