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.
但是,我还需要重用此数组,以便仅显示搜索结果数组中的唯一城镇名称。例如蒙彼利埃,里昂,雷恩,尼斯等。这个想法是当用户点击'Nice'时,只有'Nice'属性会返回。我还想显示该镇有多少房产。
The closest example as to what I want to achieve. http://www.miaandmaggie.com/dog-collars-leashes.html
关于我想要实现的最接近的例子。 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 个解决方案
#1
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.
对于country参数,只需直接传递国家/地区代码,而不是从多个国家/地区中选择很多if-else块。
$ Q-> result_array()不是和$ data一样吗?除非您期望在循环中包含条件,否则您不需要循环。
大坝也是正确的,您需要在查询中有2个(或可能更多)条件来进一步过滤结果。
我也注意到你在一张桌子上查询。您可能希望为国家,地区,属性类型,城镇等创建单独的表,以规范化架构。
#2
you need a double condition like in your sql statement
你需要一个像你的SQL语句中的双重条件
"... 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对象是如何工作的,但是根据你的问题我假设你打电话的时候
$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
我想你必须了解$ this-> db-> where where是如何工作的,并找出一种方法来为你的sql语句指定第二个条件
#1
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.
对于country参数,只需直接传递国家/地区代码,而不是从多个国家/地区中选择很多if-else块。
$ Q-> result_array()不是和$ data一样吗?除非您期望在循环中包含条件,否则您不需要循环。
大坝也是正确的,您需要在查询中有2个(或可能更多)条件来进一步过滤结果。
我也注意到你在一张桌子上查询。您可能希望为国家,地区,属性类型,城镇等创建单独的表,以规范化架构。
#2
you need a double condition like in your sql statement
你需要一个像你的SQL语句中的双重条件
"... 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对象是如何工作的,但是根据你的问题我假设你打电话的时候
$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
我想你必须了解$ this-> db-> where where是如何工作的,并找出一种方法来为你的sql语句指定第二个条件