• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Paged Result Library

#4
[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>
&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


Messages In This Thread
Paged Result Library - by El Forum - 09-25-2007, 08:59 PM
Paged Result Library - by El Forum - 09-27-2007, 11:49 AM
Paged Result Library - by El Forum - 09-28-2007, 10:33 AM
Paged Result Library - by El Forum - 09-28-2007, 02:12 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.