Welcome Guest, Not a member yet? Register   Sign In
Returning unique values of search results array
#1

[eluser]michaelfbradley[/eluser]
Hi - I'm developing a property rentals website. The search results page will contain a list of property results. It is my intention to redefine the results, say by town, country, property type etc.

So let's say for example the user searches 'France'. All of the relative properties will be returned and displayed in a list.

However, I also need to reuse this array, to display only unique town names from the search results array. e.g. Montpellier, Lyon, Rennes, Nice etc. The idea is when use user click on 'Montpellier', only the 'Montpellier' properties would return. I would also like to display how many properties are in that town.

The closest example as to what I want to achieve. http://www.miaandmaggie.com/dog-collars-leashes.html

Any ideas how I can use my search array to display the unique towns of the search?

Many thanks! M
#2

[eluser]slowgary[/eluser]
Are you using a database? I'm not entirely sure what you mean, but I'm guessing that you want to list a bunch of filters on the side of your searches, and let people refine their search results by all of those filters. Sort of how newegg.com does?

I would probably do this by determining which filters you'll want, then do a few extra "GROUP BY `town`", "GROUP BY `country`" queries for each of your filter lists. Then when a user clicks one, you'd store it in the user's session and add it to your main query. You'd also probably want to suppress selected filters from showing, based on the session data. Maybe instead of showing a selected filter, you show the selected value of that filter with a little (x) so that people could remove that filter. When they click the (x) you just delete it from the session.

If you use CodeIgniter, the session class will definitely make things easier.
#3

[eluser]michaelfbradley[/eluser]
Yes, it's all coming from a DB. I want it to work exactly as you described. http://www.miaandmaggie.com/dog-collars-leashes.html

I'm just wondering how I can go about creating the 'redefine lists'... I ideally want to know how to generate/display these values (town, property type etc) straight from the search results array. I would be inclined to use the array_unique function, only it only seems to work in a one dimensional array.

I'm pretty confident in redefining the search via the model, the stumbling block for me at the moment is reusing my search results array to display certain unique attributes (e.g. town, property type, price range etc). Thx
#4

[eluser]slowgary[/eluser]
The first thing that comes to mind for me is just using additional queries. For instance, if your search query looked like this:
Code:
SELECT * FROM `properties` WHERE `bedrooms` = 3

Each filter could issue a query like so:
Code:
SELECT `town` FROM `properties` WHERE `bedrooms` = 3 GROUP BY `town`
SELECT `country` FROM `properties` WHERE `bedrooms` = 3 GROUP BY `country`
SELECT `property_type` FROM `properties` WHERE `bedrooms` = 3 GROUP BY `property_type`
These would each give you an array of values for that particular filter. You could also use that array to count the number of results within that value, like:
Code:
Filter by Town
==============
Townville (12) //it's always nice to know how many results will be in that filter before you click it
Towningtonne (17)
Towneroo (11)

Your price range filter would be a little more fixed, so you'd probably just need to determine the ranges and create those links manually.

You could potentially do this with your initial result array, but I think you'd need to resort the array by the filter column each time and it would lead to a lot of additional looping and logic that would not only be a lot of extra work, but likely slower than just using MySQL for a few extra queries.

That's just how I'd do it anyways.




Theme © iAndrew 2016 - Forum software by © MyBB