CodeIgniter Forums
Paged Result Library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: Paged Result Library (/thread-3351.html)



Paged Result Library - El Forum - 09-25-2007

[eluser]wiredesignz[/eluser]
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.

Code:
<?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;
  }
}
?&gt;

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)


Paged Result Library - El Forum - 09-27-2007

[eluser]vijinho[/eluser]
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


Paged Result Library - El Forum - 09-28-2007

[eluser]marcoss[/eluser]
[quote author="vijinho" date="1190933389"]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[/quote]

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


Paged Result Library - El Forum - 09-28-2007

[eluser]vijinho[/eluser]
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.

<code>
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
[urlpath] => 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
)

)
</code>

<code>
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Fail Pager Helpers
*
* @package Fail
* @subpackage Helpers
* @category Helpers
* @author Vijay Mahrra <[email protected]>
* @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;
}

// ------------------------------------------------------------------------
?&gt;
</code>

jay