Pagination for search module ?
Posted: Mon Feb 25, 2013 3:42 pm
Anyone managed to do pagination for the search module ?
Content management as it is meant to be
https://forum.cmsmadesimple.org/
Code: Select all
# Get parameters from URL and UDT call
$search_page = $_GET['search_page'] ? $_GET['search_page'] : 1;
$results_per_page = $params['results_per_page'] ? $params['results_per_page'] : 10;
$results = $params['results'];
$results_assign = $params['results_assign'];
$pagelinks_assign = $params['pagelinks_assign'];
# Chop off the beginning of the array based on page number and count the results remaining.
$num_results = count(array_slice($results, ($search_page - 1) * $results_per_page));
# Slice and dice the results array based on number of results per page and page number
$results = array_slice($results, ($search_page - 1) * $results_per_page, $results_per_page);
$pagelinks = new stdClass;
# link to prev page;
if ($search_page > 1) {
$get = $_GET;
$get['search_page'] = $search_page - 1;
$pagelinks->prev = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '?' . http_build_query($get);
}
# link to next page;
if ($num_results > $results_per_page) {
$get = $_GET;
$get['search_page'] = $search_page + 1;
$pagelinks->next = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '?' . http_build_query($get);
}
# dump to smarty
$smarty->assign($results_assign, $results);
$smarty->assign($pagelinks_assign, $pagelinks);
Code: Select all
{search_paginate results_per_page=10 results=$results results_assign=results pagelinks_assign=pagelinks}
Code: Select all
{if isset($pagelinks->prev)}
<a href="{$pagelinks->prev}">Previous</a>
{/if}
{if isset($pagelinks->next)}
<a href="{$pagelinks->next}">Next</a>
{/if}