【问题标题】:Real estate site, trying to only output properties based on type房地产网站,尝试仅根据类型输出属性
【发布时间】:2025-12-28 13:35:07
【问题描述】:

所以我从另一个开发人员那里继承了一个旧网站,而我并不是真正的程序员,所以我遇到了一些麻烦。我已将代码放入 Fiddle: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>

它只是在每一页上应用“全部”。同样,小提琴具有完整的 php 和 html,这可能更有帮助。我意识到这很丑陋而且很糟糕,但也许有人能看到一些我看不到的明显的东西。

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

【问题讨论】:

  • 请再看看您发布的小提琴。有点乱。
  • 这在我看来像是 PHP 代码,而不是 JavaScript - 这就是它在 JSFiddle 中呈现不佳的原因。
  • 是的,我有点说我不是程序员。我也没有写任何代码。我的任务是弄清楚为什么它没有正确过滤。我其实是一个前端设计师。也许我应该找其他人帮忙。这有点过头了。我只是希望这里有人能看到一些明显的东西。

标签: php


【解决方案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'];

【讨论】:

  • 太棒了,这解决了它!我现在知道 Fiddle 不是发布代码的最佳位置。不过,我不知道有类似的 PHP 站点。有吗?无论如何,非常感谢!我很感激。
  • @ViPeRx007 以下是我找到的一些建议*.com/questions/3869226/…*.com/questions/4616159/…
【解决方案2】:

您的 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

这种布尔方法总是会带回 其中一个,因此每次都会带回“全部”。

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

处理此问题的最简单方法是将值 $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

【讨论】: