房地产网站,试图只输出基于类型的属性

时间:2015-08-07 19:36:31

标签: php

所以我从另一位开发者那里继承了一个旧网站,我并不是一个真正的程序员,所以我遇到了麻烦。我已将代码放入小提琴:https://jsfiddle.net/s6coraf5/

基本上有不同类别的房地产属性,当您点击不同的页面时,它应该过滤它们,只显示特定于您所在页面的页面。问题在于,无论您在哪个页面上,它都只是显示所有内容。我已经缩小了一些特定的代码,但无法弄清楚它为什么没有正确应用它。

在php中:

$select_title = "Unknown";      
    if ($select_type == "all") { $select_title = "All Listings"; }  
    if ($select_type == "office") { $select_title = "Office"; }     
    if ($select_type == "industrial") { $select_title = "Industrial"; }     
    if ($select_type == "retail") { $select_title = "Retail"; }     
    if ($select_type == "shoppingcenter") { $select_title = "Shopping Center"; }    
    if ($select_type == "land") { $select_title = "Land"; } 
    if ($select_type == "agricultural") { $select_title = "Ranch / Farm"; } 
    if ($select_type == "investment") { $select_title = "Investment"; }     
    if ($select_type == "lodging") { $select_title = "Lodging"; }   
    if ($select_type == "sportsentertainment") { $select_title = "Sports /      Entertainment"; }

在HTML中,有多个地方可以应用这些$ select_type:

a href="properties.php?select_type=<?php echo $select_type;?>&select_city=<?php echo $select_city;?>&priceForm=<?= $lowPrice;?>,<?= $highPrice; ?>&sqft=<?= $lowSize;?>,<?= $highSize; ?>&sort_type=city, asking_price desc"><font size=4><b>Location,</b></a>

它只应用&#34; all&#34;每一页都有一个。再次,小提琴有完整的PHP和HTML可能更有帮助。我意识到它的丑陋和糟糕,但也许有人可以看到一些我无法做到的事情。

提前感谢任何人提供的任何帮助。

2 个答案:

答案 0 :(得分:1)

基于小提琴中的PHP代码(因为小提琴是针对Javascript的,实际上不应该存在),似乎问题是你从不使用URL中给出的select_type

看看这一行。这是第一次使用$select_type

if (!isset($select_type)) $select_type = "all";

因此,$select_type始终为all。 相反,您应该将其更改为:

if (!isset($select_type)) $select_type = $_GET['select_type'];

或者只是在它之前添加这一行:

$select_type = $_GET['select_type'];

答案 1 :(得分:0)

你的jsfiddle中的ssql查询似乎可能是罪魁祸首。我会把它放在这里以使其更容易:

select properties.property_id,selected_subtypes.property_type,properties.listing_type,properties.city,properties.asking_price,memberships.name,properties.membership_id,properties.building_size,memberships.website,properties.sold_price
                     from selected_subtypes,properties,memberships where (selected_subtypes.property_id = properties.property_id) 
                      and (properties.membership_id = memberships.membership_id)
              and (memberships.status = 'Active') and (properties.sold_information = ' ' or properties.sold_information = 'Undisclosed')
                      and ((selected_subtypes.property_category ='".$select_type."' or '".$select_type."'='all')
                      or (selected_subtypes.property_type ='".$select_type."'))
                      and (properties.city = '".$select_city."' or '".$select_city."'='all') 
                      and (properties.asking_price BETWEEN ".$lowPrice." and ".$highPrice.")
                      and (properties.building_size BETWEEN ".$lowSize." and ".$highSize.") 
                      ".$date_sql."
                      order by ".$sort_type

在每一行中,查询似乎都是选择$select_type OR 'all

这种布尔方法总会带回 ,所以每次都会带回“all”。

如果您只想恢复所选类型,则需要删除这些行中OR中的“all”。

处理此问题的最简单方法是将值$ select_type设置为等于“all”(如果选择的话),或者是特定类型。我做“所有”查询的一种方法是将值设置为“1 = 1”,这将始终为真。

换句话说,像这样修改查询(对于每个选定的类型)来显示这个(在这种情况下我将OR更改为AND)

AND selected_subtypes.property_type ='".$select_type."'

然后在php中将代码修改为:

if (!isset($select_type)) {
   $select_type = "1=1"
} 
else {
   $select_type = $_GET['select_type'];
}

需要注意的另一件事

此特定代码在某种程度上容易受到SQL注入攻击,因此您可能希望修改查询数据库的方式。我强烈建议您使用mysqliPDO

查看准备好的语句
相关问题