gpt4 book ai didi

php - 使用 php 跨选择进行搜索过滤

转载 作者:行者123 更新时间:2023-11-30 23:20:48 26 4
gpt4 key购买 nike

我似乎无法找到制作搜索过滤函数的正确方法,该函数会将(正确的词?)扩展的 WHERE 语句推送到 SELECT 查询。

我有一段显示广告列表的代码:

    $where = isset($catid) ? ' AND ad.category = '.$catid.'' : '';

$row = $db->dbh->query('SELECT ad.*,
(SELECT img.image FROM '.$config->db_prefix.'_images AS img
WHERE img.aid = ad.aid LIMIT 1) AS img
FROM '.$config->db_prefix.'_adverts ad
WHERE ad.approved = 1 '.$where.'');

假设我想查看特定类别 f.x 中的所有广告。电视 (id 5),然后在该类别中我想显示给定价格范围 (100 - 800) 内的所有广告。我不确定如何扩展我的 WHERE 语句来处理这个问题以及我应该如何从我的 HTML 中做到这一点。希望我的问题是可以理解的。

我在广告表中的数据库结构是这样的:

aid             int(11)     
fid int(11)
ad_type int(11)
title varchar(255)
text tinytext
price decimal(10,2)
category int(11)
friends_allow int(11)
cr_date int(11)
expiry_date int(11)
approved int(1)
ip varchar(255)
email varchar(255)
zip_for_pickup int(11)
views int(11)

解决方案

$filters = array();

if(!empty($post_fields['cat']) && $post_fields["cat"] != "all") {
$category = $post_fields['cat'];
$filters[] = '(category = ' . $category .')';
}

if(!empty($post_fields['search_field'])) {
$title_and_text = $post_fields['search_field'];
$filters[] = '(title LIKE "%' . $title_and_text .'%" OR text LIKE "%' . $title_and_text .'%")';
}

if(!empty($post_fields['price_min_field'])) {
if(!empty($post_fields['price_max_field'])) {
$filters[] = '(price >= '. $post_fields['price_min_field'].' AND price <= '. $post_fields['price_max_field'] .')';
} else {
$filters[] = '(price >= '. $post_fields['price_min_field'].')';
}
}

if(!empty($post_fields['distance'])) {
$adverts = new Adverts();
$filters[] = '('. $adverts->find_geo_location($post_fields["userzip"], $post_fields["distance"]) .')';
}

$filter = implode(' AND ', $filters);

最佳答案

在 HTML 中放置 2 个下拉列表价格从和价格到。在 PHP 端获取选定的值。基于那个使用串联。将 100 和 800 替换为所选值。

 $where = isset($catid) ? ' AND ad.category = '.$catid.'' : '';

if ($pricefilter) {
$where .= 'AND ad.price>100 AND ad.price < 800';
}

关于php - 使用 php 跨选择进行搜索过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15521131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com