views:

83

answers:

2

I'm creating a page that's a search result...

When you're viewing one of the results, at the bottom of the page, I need to insert "Next result" and "Previous result" links, like a pagination... But from [I think] a saved search, right?

How you people would do this?

Obs.: I'll use CakePHP (PHP) and MySQL

--

Update: The problem is not how to paginate the search results like "20 results per page"... I need to jump from one record to another, sequentally in the same search result.

Let's say that I found the records 3, 5, 8 and 9 with my search... Then I click to view the #5 record and I can click on the "next result" link to jump to #8 record.

--

Possible solution #1

Create a array with the results IDs like:

$results = array(3, 5, 8, 9);

And store this array in the session or cookies with a token (SHA1, MD5 or UUID) and pass this token as GET param in the URL.

A: 

I dunno about cake, but all you have to do - is use GET method for the search form and preserve query string values in the URL.

In plain PHP I'd use http_build_query() function, say,

if (isset($_GET['page'])) unset($_GET['page']);
$qs=http_build_query_string($_GET);
$url="?$qs&page=$page";

as for the pagination code, you can find nearly thousand topics here on SO

Col. Shrapnel
Did you mean `http_build_query()`?
TiuTalk
oh yeah, thanks
Col. Shrapnel
Saving the search string on a session is an option. A lousy option though.
Jorge
Please, read my update.
TiuTalk
+1  A: 

Using query limits and offsets. For example, on page 1 the offset is 0, and the limit is the amount of results you want. As the pagination progresses,so does the offset. The offset states that you want to retrieve a determined ammount of results starting from the 11th results, for example.
I never used CakePHP, but this article seems to explain well how to do the pagination.

Jorge
Please, read my update.
TiuTalk
Oh, i see. What comes to my mind is: build an array and populate it with the retrieved results. Then you just need to iterate on it normally to get the next relevant result. I don't know how to store the array between page-jumps though.
Jorge
@Jorge - Yeah... That's I think it's the best solution... Maybe in the session? Cookies? Or the worst... Database? :S
TiuTalk
Not database because then you'd have to make a new query. What about this? http://api13.cakephp.org/class/cache#method-Cacheread(Alô gambiarra) :D
Jorge
@Jorge - Take a look to my "Possible solution #1" and tell me what do you think
TiuTalk
@TiuTalk The thing is...if you only store the ids, you'll have to query the database on every new page. I think fetching it once and storing the whole result set in the cache is more effective, because you'll have the data already fetched and parsed as needed.The MemcacheEngine class looks like a feasible solution: query just once and cache the results. http://api13.cakephp.org/class/memcache-engine
Jorge
I would say that paging through say 50 results 1 result/page is not that much different than paging with 20 results/page. ;)
hangy