Welcome Guest, Not a member yet? Register   Sign In
How to implement complex search?
#1

[eluser]nofearinc[/eluser]
Hello everyone,

I have a search module that I cannot implement properly. For a real estate site I have to implement advanced search via 15 different filters. Results have to be visible as table with pagination.

Currently I have filtering and search working and filling the table with the results. My problem is passing the search parameters in the session. I use flashdata to pass all the data page by page. But if I click on details or open another tab for other search, my data is lost.

I have 3 ideas to implement it:

1. Permit using GET queries and pass parameters in the URL. It's totally not CI friendly indeed.
2. Save params in the session object, not flashdata. Then I don't know how to flush the session, also I have to set different IDs for different sessions (if we open few tabs and do parallel search)
3. Create a DB table for sessions and save data there.

Is there any popular solution to use here?
#2

[eluser]Sean Gates[/eluser]
I'm assuming your filters have to do with min. price, max. price, bedrooms, etc.?

You could get by using delimited values like:

Quote:http://site.com/search/index/data-to-be-...-a-dash/P0

That would get you around the GET issue, as well as give you state when returning to the search page.
#3

[eluser]nofearinc[/eluser]
I have undefined count of parameters and different value types (also I use special symbols in search that I have to escape), so I actually think it's going to be hard to parse and set the params in URL, too. It would probably be easier if I knew the number of parameters I pass, but I don't.

I think I'm going to try serializing parameters and save persist them in database with a session ID which I'm going to use. Then I'm going to flush this table once a week in order to keep it clean. Honestly I prefer another solution, but still don't find prettier one...
#4

[eluser]Sean Gates[/eluser]
Yes, a database solution using sessions is probably your best bet.

BTW, how come you don't know the number of filter parameters? It's not fixed for your data set? Can the data set be modified? Are you using tagging to describe the data?
#5

[eluser]nofearinc[/eluser]
Well, I have a large number of parameters but I am able to filter all of them or just a few. Also I have 6 different advanced search pages quering the same database. Currently I just check manually all the parameters I know and then add the constraints via Active Record, f.i.

To be clear, I have 6 types of offers with different params in a parent (and sub) tables. I do DB join and check the incoming parameters - if not empty, do a where clause to filter the results.

Code:
if(!empty($someparam)) {
$this->db->where('column', $someparam);
}

That's just an example, but that's the point now.
#6

[eluser]Ben Edmunds[/eluser]
I would simply save it as a multi-dimensional array in the $_SESSION['filter1']['blah'] and unset($_SESSION['filter1']) when I'm done.
#7

[eluser]nofearinc[/eluser]
What I'm interested is what does it mean "when I'm done"? The business case is that user can have 2-3 tabs open, one of them searching, 1 publishing offer and 1 checking statistics. At the same time. So I'm not sure when I have to free the session at this point. Using DB and serialize() method at least gives autoincrement ID that can be flushed with cron task or something.

The paging and passing parameters is the worst coded part of my project and I sincerely try to improve it, so thank you all for the efforts Smile Ideas and snippets are highly appreciated.
#8

[eluser]nofearinc[/eluser]
My final decision was the database driven one - I created a 'search_sessions' table with id, query (serialized rezult) and query_date. My search button navigates to prepare_search method that serializes all the parameters to the database and assigns the session_id to the URL. Then pagination is applied and everything is OK.

Now I have persistent search, my back button is working, few tabs are working simultaneously and I am able to pass the URL to someone else to check the data.

Thanks for the advices Smile
#9

[eluser]viisik[/eluser]
would you share some code in some way how you did it - i need similar coplex search

how controllers and views are related
#10

[eluser]nofearinc[/eluser]
I lost my post twice on post, so I'll keep it short.

2 controllers: Search and Result, 2 views: for search form and results table, and one model for my offers.

Search is just search form with fields you have to fill for filter - nothing unusual. It redirects to result/prepare_sell (for sell offers or prepare_X for other type X)

Snippet for prepare_sell:

Code:
// get all input params (here I show just 4 for example)
            $min_area = $this->input->post('min_area');
            $max_area = $this->input->post('max_area');
            $min_price = $this->input->post('min_price');
            $max_price = $this->input->post('max_price');  

// add them to array            
            $search_params = array(
                                    'min_area' => $min_area,
                                    'max_area' => $max_area,
                                    'min_price' => $min_price,
                                    'max_price' => $max_price);
            
// serialize them in JSON format
            $ser = serialize($search_params);
            // add them to sessions database
            $session_record = array(
                'query_text' => $ser,
                'search_date' => date('Y-m-d H:i:s')
            );
            $sess_id = $this->offers->add_search_session($session_record);

I've skipped all validations and most of the fields, but this is the algorithm I use.
we redirect to results/sell and pass the $sess_id as a param:

Code:
function sell($session_id = '') {
            // get session from database via the session_id we pass
            $session = $this->offers->get_search_session($session_id);
            // set validation rules
            
            $per_page = 20;
            $base_url = site_url('results/sell/'. $session_id);
            $config['uri_segment'] = 4;
            $config['num_links'] = 12;

// deserialize text query and add parameters to session_record array            
            $ser = $session['query_text'];
            $session_record = unserialize($ser);
            // add the array in the flashdata and then just print fields in the view
            $this->session->set_flashdata($session_record);

Note I use URI segment = 4 (site.com/result/sell/session/page), and so I have the session and the page in the URL.

Pagination I do setting offset in the URL and limit hardcoded in the config.




Theme © iAndrew 2016 - Forum software by © MyBB