Filter database results without running an additional query

I am using CodeIgniter for an ad site. Here is a concrete example of what I'm trying to achieve:

The page http://www.example.com/browse/Real-Estatelists all real estate listings. I use the CI Pagination Class to break down the results.

The left pane shows a set of filters corresponding to each db column of the "Real Estate Ads" table. For instance:

Type: House, Apartment Bedrooms: 1,2,3,4 ... Furnished: Yes, No, etc.

Each time the user selects a value, a query is executed and the results are updated using Ajax.

However, since the choice of the value corresponds to the optional WHERE clause in the original query, I was wondering what would be the best way to filter the results already available without restarting the query.

+3
source share
2 answers

Why are you against running an additional request? If a user starts with a broad query without filters, filtering it will mean repeating the result set and performing a filter check on each row. If the user starts with a narrow query with filters, changing or deleting the filters will in any case require a new query, and iterating over a set of results will be required to add filters.

, , , , , HTML jQuery ( - ), . , Javascript , , ( Javascript ).

+1

select * from TABLE

, - , ajax, .

ajax -

function update_criteria(){
//Here you can call an another function say aply_criteria as follows.

array_walk('your actual result array','apply_criteria');

//finally parse the html with new filtered result throuh ajax.

 echo $this->parser->parse('view_name','data array',TRUE);

//catch the above html in the sucess callback function of ajax and replace it in the 
//previous html page.

Note :- the parser i have used above is an library in an codigniter so make sure you load it before using.

}

function apply_criteria(){

//Actual logic to unset the specific key value pair 
//(which does not satisfy the  criteria depending on the post data) from array.

}

array_walk - http://www.php.net/manual/en/function.array-walk.php

+1

Source: https://habr.com/ru/post/1769781/


All Articles