Paged Result Library |
[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 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)
[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
[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?
[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/...es/listing [urlpath] => http://home.example.com/index.php/admin/.../country/1 [links] => Array ( [1] => http://home.example.com/index.php/admin/...try/1/1/10 [2] => http://home.example.com/index.php/admin/...try/1/2/10 [3] => http://home.example.com/index.php/admin/...try/1/3/10 [4] => http://home.example.com/index.php/admin/...try/1/4/10 [5] => http://home.example.com/index.php/admin/...try/1/5/10 [6] => http://home.example.com/index.php/admin/...try/1/6/10 [7] => http://home.example.com/index.php/admin/...try/1/7/10 [8] => http://home.example.com/index.php/admin/...try/1/8/10 [9] => http://home.example.com/index.php/admin/...try/1/9/10 [10] => http://home.example.com/index.php/admin/...ry/1/10/10 ) ) </code> <code> <?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; } // ------------------------------------------------------------------------ ?> </code> jay |
Welcome Guest, Not a member yet? Register Sign In |