2

I have small problem.

I've coded a full website in php using CodeIgniter framework. One of my modules is search module, it contains text input with keyword and three select lists with filtering criterias.

That's ok, when I'm searching something - result's listing pagination is done via URL like that:

mysite.com/$keyword/$criteria1/$criteria2/$criteria3/$offset

works like a charm.

But when I'm entering into one of my images (it's image gallery) I want to have an option to go into NEXT and PREVIOUS image from my search results - the ones which I entered this image from.

I'm solving this case now in this way - I have session table called 'search_conditions' and I'm storing values of keyword and my three criterias there, but that's quite not comfortable, because why if someone opens second window and search something else there?

Then all of his searches in another windows or tabs are getting the same criteria - because with every new search, user overwrite the session value.

My next and previous functions:

public function next($count)
{
    $search = $this->session->userdata('search_conditions'); //getting session table and overwriting it

    $catid = isset($search['catid'])?$search['catid']:'0';
    $brandid = isset($search['brandid'])?$search['brandid']:'0';
    $prodid = isset($search['prodid'])?$search['prodid']:'0';
    $keyword = isset($search['keyword'])?$search['keyword']:'';

    $res = $this->search_model->main_search($keyword,  $catid, $brandid, $prodid, $count, 1);       
}



public function previous($count)
{
    $search = $this->session->userdata('search_conditions');

    $catid = isset($search['catid'])?$search['catid']:'0';
    $brandid = isset($search['brandid'])?$search['brandid']:'0';
    $prodid = isset($search['prodid'])?$search['prodid']:'0';
    $keyword = isset($search['keyword'])?$search['keyword']:'';

    $res = $this->search_model->main_search($keyword,  $catid, $brandid, $prodid, $count-2, 1);     

}

Can you recommend me some other, more comfortable solution, because this seems not to be good... : )

Thank you!

pawel
  • 5,976
  • 15
  • 46
  • 68

2 Answers2

2

Pull the segments from the URI in your next() and previous() functions. Use the codeigniter URL helper. That should allow you to pass the different search criterion as variables to the next page, this would also remove your need to use the session.

korono89
  • 216
  • 2
  • 4
  • I thought about it, and my urls will look like `site.com/image/next/keyword/crit1/crit2/crti3/offset`, that's ok - but how to pass these criterias into my image page? With url too? It looks ugly : ) – pawel Aug 06 '12 at 22:06
2

Add an index to the $search_conditions variable:

$search_conditions[1]['catid']
$search_conditions[1]['brandid']
...

then refer to it with a controller's or config variable. This way you can allow one session to store multiple search conditions.

But I would recommend you drop storing the search condition in session. Instead, just pass it with the URI. Session data, in the case you describe, work as an intermediary; you don't need it. Use the Pagination Class and pass the search page number, not the direction (next or previous) to the URI.
Do not worry that the URI may look ugly - it only depends on what user searches for, and it's still friendly to share. Your only concern is if the GET string does not extend the limited length.

Community
  • 1
  • 1
Taz
  • 3,718
  • 2
  • 37
  • 59
  • Yep, I'm trying to do it : ) My URL will look like: mysite.com/image/id/keyword/crit1/crit2/crit3/offset : ) - pretty long, huh? – pawel Aug 06 '12 at 22:52
  • @pawel Not too bad. Look at how do SO or Google search URIs look like :) – Taz Aug 06 '12 at 23:01