編輯:我發現我的原始代碼中的一個大缺陷。對於我的選擇,我對「full_name」和「city」使用了相同的變量名稱(複製和粘貼錯誤)。但我仍然不確定如何輸入select語句來根據兩個下拉菜單選擇行。我在下面發佈了我的兩部分代碼。有任何想法嗎?謝謝!多下拉查詢過濾器
下拉框代碼:
<select name="name">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY full_name ORDER BY full_name";
$sql_result = mysql_query ($sql, $connection) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["full_name"]."'".($row["full_name"]==$_REQUEST["full_name"] ? " selected" : "").">".$row["full_name"]." </option>";
}
?>
</select>
<label>City</label>
<select name="city">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY city ORDER BY city";
$sql_result = mysql_query ($sql, $connection) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["city"]."'".($row["city"]==$_REQUEST["city"] ? " selected" : "").">".$row["city"]."</option>";
}
?>
</select>
,並選擇行代碼:
<?php
if ($_REQUEST["city"]<>'') {
$search_city = " WHERE city='".mysql_real_escape_string($_REQUEST["city"])."'";
}
if ($_REQUEST["full_name"]<>'') {
$search_full_name = " AND full_name='".mysql_real_escape_string($_REQUEST["full_name"])."'";
}
if ($_REQUEST["from"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"].$search_string.$search_city.$search_full_name;
} else if ($_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"].$search_string.$search_city.$search_full_name;
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"].$search_string.$search_city.$search_full_name;
}
$sql_result = mysql_query ($sql, $connection) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>
我選擇了你的方法,但只注意到了我的代碼中的一個致命缺陷。我爲「城市」所做的「full_name」使用相同的變量,因此「full_name」會被覆蓋?我認爲。看到我上面的修訂。我也會將from_date和to_date簡化一下。 – 2012-03-24 01:23:05