views:

1334

answers:

2

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 'Nice', only the 'Nice' 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

A: 

you need a double condition like in your sql statement

"... where `country`='France' and `region`='Lyon'"

I don't know how your $this->db object works but from your question I assume that when you call

$this->db->where('country',$country_code);

this overwrites what you did some lines above:

$this->db->where('region',$region);

and the final result is that you get all the properties in France not only in Lyon

I think you have to understand how $this->db->where works and find out a way to specify a second condition to your sql statement

--
dam

dam
This is not true - CodeIgniter's ActiveRecord allows you to call where more than once (it does not overwrite).
jimyi
thank you jiimyi for the note but infact I said "I don't know how your $this->db works"
dam
Anyway does Michael Bradley need to specify an "AND" condition for the where clause?
dam
Nope. Here is the documentation for it: http://codeigniter.com/user_guide/database/active_record.html
jimyi
After reading well his question I think he is not complaining about a not working code. He probably wants to show distinct cities when someone picks only the country without showing regions. So he just needs a query with a distinct=> <i>"So let's say for example the user searches 'France' - I need an array to return only the unique town names from the search results array. e.g. Montpellier, Lyon, Rennes, Nice etc."</i>I would like to ask but I have not enough reputation to add a comment to the question
dam
Hi Dan - much appreciated. $this->db->distinct() did the trick.
Michael Bradley
A: 

A couple of things I noticed:

  • For the country parameter, just pass the country code directly instead of having a lot of if-else blocks to choose from several countries.
  • Isn't the $Q->result_array() the same as $data? Unless you're expecting to include a condition inside the loop, you don't need to have the loop.
  • dam is also right that you need to have 2 (or probably more) conditions in your query to further filter your results.
  • I also noticed that you're querying from a single table. You might want to create separate tables for the country, region, property type, town, etc. to normalize your schema.
Randell