pagination problems - left in the lurch by my tech guy
Posted: Sat May 30, 2009 9:39 pm
Hi, my tech guy left my project on short notice. Until I find someone to replace him (and money to do so) he has also left me some bugs to fix. My knowledge of php is nil but happy to learn. We have a paginator on the site that does not store/cache your page number which means that if a member of our site is on page 3 of 9 say in the div and click a result and then wish to go back they do not go back to page 3 of 9 using the back function on their browser (they always return to page 1!).
Code is below: all help appreciated. ps: I assume this is being done using the below file...it was labelled pagination.php on our server. Could it be a javascript issue? an example can be found at the bottom of the page on http://www.tailcast.com/art.html
Thanks
Code is below: all help appreciated. ps: I assume this is being done using the below file...it was labelled pagination.php on our server. Could it be a javascript issue? an example can be found at the bottom of the page on http://www.tailcast.com/art.html
Thanks
Code: Select all
* Build the pagination data
*
* @param integer $nr_results The number of displayed items
* @param array $params (optional) The parameters variable contains 2 things:
* - pagination: page, show, sort, order
* - sort_filter: array(....) - the default sort value will be the first element of this array
* @param string $query_hash (optional) Query hash, ie: #comments. Remember to add the # too
* @param array $private_params (optional) Private params, not passed in the pagination links
* @return array
*/
public function init($nr_results, $params=null, $query_hash=null, $private_params=null)
{
// set the pagination elements: page, show
if (empty($params['pagination']['page']))
{
$params['pagination']['page'] = 1;
}
if (empty($params['pagination']['show']))
{
$params['pagination']['show'] = DEFAULT_ITEMS_PER_PAGE;
}
// set the pagination elements: sort
if ($params['pagination']['sort'] && array_search($params['pagination']['sort'], (array)$params['sort_filter']) === false)
{
unset($params['pagination']['sort']);
}
if (empty($params['pagination']['sort']))
{
$params['pagination']['sort'] = $params['sort_filter'][0];
}
unset($params['sort_filter']);
// set the pagination elements: order
if ($params['pagination']['order'] != 'asc' && $params['pagination']['order'] != 'desc')
{
$params['pagination']['order'] = 'asc';
}
// set the "sort" and "limit" parameter values
$params['sort'] = $params['pagination']['sort'].' '.$params['pagination']['order'];
$params['limit'] = ($params['pagination']['page'] - 1) * $params['pagination']['show'].', '.$params['pagination']['show'];
// count the number of pages
$params['pagination']['nr_results'] = $nr_results;
$params['pagination']['nr_pages'] = ceil($params['pagination']['nr_results'] / $params['pagination']['show']);
// build the pages and return the entire pagination data
return $this->buildPages($params, $query_hash, $private_params);
}
/**
* Build the pages
*
* @param array $params Pagination parameters
* @param string $query_hash (optional) Query hash, ie: #comments. Remember to add the # too
* @param array $private_params (optional) Private params, not passed in the pagination links
* @return array
*/
protected function buildPages($params, $query_hash, $private_params)
{
// check for extra pagination params
foreach ((array)$params['pagination'] as $key => $var)
{
if ($key != 'page' && $key != 'sort' && $key != 'order' && $key != 'show' && $key != 'nr_results' && $key != 'nr_pages')
{
$extra_vars[$key] = $var;
}
}
// build the base_url and sort_link
$url =& Singleton::initController('url');
$base_url = $url->buildGetUrl(array('page'), $extra_vars);
$params['pagination']['sort_link'] = $url->buildGetUrl(array('page','sort','order'), $extra_vars);
// set the private parameters
foreach ((array)$private_params as $key => $value)
{
$params['pagination'][$key] = $value;
}
// if we don't have more then one page, return the results
if ($params['pagination']['nr_pages'] <= 1)
{
return $params;
}
// build the first page
if ($params['pagination']['page'] > 1)
{
$params['pagination']['first_page'] = $base_url.'page=1'.$query_hash;
}
// built the last page
if ($params['pagination']['page'] < $params['pagination']['nr_pages'])
{
$params['pagination']['last_page'] = $base_url.'page='.$params['pagination']['nr_pages'].$query_hash;
}
// build the previous page
if ($params['pagination']['page'] > 1)
{
$params['pagination']['previous_page'] = $base_url.'page='.($params['pagination']['page']-1).$query_hash;
}
// build the next page