Page 1 of 1

Pagination for search module ?

Posted: Mon Feb 25, 2013 3:42 pm
by applejack
Anyone managed to do pagination for the search module ?

Re: Pagination for search module ?

Posted: Thu Mar 14, 2013 10:37 pm
by Wishbone
It's possible, I guess.. Seems like you can call a UDT in the search template that reads a 'searchpage' variable from the URL, and truncates the output based on the page number and pre-set number of records returned.. Previous and Next buttons would link to the same search results, but with a different 'searchpage' .. This would probably only work with search in 'get' mode.

Re: Pagination for search module ?

Posted: Fri Mar 15, 2013 12:01 am
by applejack
Hi Wayne

Hope you are well and not heard from the one who shall not be named !!!

Anyway I am seriously disappointed with your none answer given the Intel / NASA Scientist you purport to be !!!

Re: Pagination for search module ?

Posted: Sat Mar 16, 2013 4:43 am
by Wishbone
Jeez.. If you put it that way, I have a procedure for you.

This is tested on an old version (1.9.4.1) of CMSMS that I have lying around. Not tested with pretty URLs.

Note that for this to work, your {search} must be in 'get' mode. In this version, {search search_method='get'} still puts it in post mode. You need to remove the parameter, as {search} defaults to 'get'. Don't know if this bug has been fixed since.

Create a UDT called 'search_pagination' containing:

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);
In the beginning of your results template, put a call to:

Code: Select all

{search_paginate results_per_page=10 results=$results results_assign=results pagelinks_assign=pagelinks}
In the call above we're saying we want 10 results per page, use the $results array, overwrite the $results array with our paginated results, and create a $pagelinks variable containing our prev and next URLs.

At the end of your results template, create the prev and next links:

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}
Here's a working example:

http://1941.teamwishbone.com/

Type something like 'modules' in the search.. 4 results per page.

Pretty basic.. No error checking.