我似乎无法找到正确的方法来制作一个将(正确的词?)扩展的 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.'');
假设我想查看特定类别 fx TV (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);