php三下拉过滤器
我有一个名为classifieds的表,它包含以下内容:php三下拉过滤器,php,Php,我有一个名为classifieds的表,它包含以下内容: 身份证 头衔 描述 州id 城市标识 猫咪 这是从数据库动态填充的 <form name="theForm" method="post" action="advanced_search.php"> <input type="submit" name="Submit" value="Search" /> <select name="categories" > <o
- 身份证
- 头衔
- 描述
- 州id
- 城市标识
- 猫咪
<form name="theForm" method="post" action="advanced_search.php">
<input type="submit" name="Submit" value="Search" />
<select name="categories" >
<option value=""> Select Category</option>
</select>
<select name="state" >
<option value=""> Select state</option>
</select>
<select name="city" >
<option value=""> Select city</option>
</select>
...
有人能给我介绍一下吗?只有在他们选择了以下内容时才添加where子句:
$select = "SELECT * FROM classifieds";
$where = " WHERE ( authorized = '1')";
if($categories) {
$where .= " AND (cat = '" . $categories . "' )";
}
if($state) {
$where .= " AND (state_id = '" . $state . "' )";
}
if($city) {
$where .= " AND (city_id = '" . $city . "' )";
}
$q = $select . $where . ' ORDER BY date DESC';
要求“演练”并不适合堆栈溢出;你能把你的问题集中在当前阻碍你进步的具体问题上吗?你是否去掉了“
$where.=”和(cat=”、“$categories.”和state_id=”、“$state.”和city_id=”、“$city.”)代码>?我编辑了我的答案,以提供生成查询的完整代码。仅当我选择类别、州和城市时,这才有效。但是如果我没有选择一个城市,那么结果显示为0。如果我只选择类别而不选择州和城市,则相同。您没有更改表单代码,是吗?我的代码假设“选择…”选项的值为空字符串。如果($categories!=“”){$where.=”和(cat=“$categories.”“;}等等,我就是这样做的。我整天都在玩这个,但我不能得到我想要的。
$select = "SELECT * FROM classifieds";
$where = " WHERE ( authorized = '1')";
if($categories) {
$where .= " AND (cat = '" . $categories . "' )";
}
if($state) {
$where .= " AND (state_id = '" . $state . "' )";
}
if($city) {
$where .= " AND (city_id = '" . $city . "' )";
}
$q = $select . $where . ' ORDER BY date DESC';