2012-03-23 79 views
1

編輯:我發現我的原始代碼中的一個大缺陷。對於我的選擇,我對「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>&nbsp;&nbsp; 
<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)) { 
?> 

回答

0

看起來一目瞭然,你只需將字符串

$search_city = ''; 
$search_full_name = ''; 

if ($_REQUEST["city"]<>'') { 
$search_city = " AND 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"]." WHERE from_date >= '".mysql_real_escape_string($_REQUEST["from"])."'".$search_string.$search_city.$search_full_name; 
} else if ($_REQUEST["to"]<>'') { 
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE to_date <= '".mysql_real_escape_string($_REQUEST["to"])."'".$search_string.$search_city.$search_full_name; 
} else { 
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$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)) { 
+0

我選擇了你的方法,但只注意到了我的代碼中的一個致命缺陷。我爲「城市」所做的「full_name」使用相同的變量,因此「full_name」會被覆蓋?我認爲。看到我上面的修訂。我也會將from_date和to_date簡化一下。 – 2012-03-24 01:23:05

0

你可以使用一個用於動態添加條件到查詢的數組。喜歡的東西 -

$criteria = array(); 

if ($_REQUEST['city'] <> '') { 
    $criteria['city'] = $_REQUEST['city']; 
} 

if ($_REQUEST['full_name'] <> '') { 
    $criteria['full_name'] = $_REQUEST['full_name']; 
} 

if ($_REQUEST['from'] <> '') { 
    $criteria['from_date'] = array($_REQUEST['from'], '>='); 
} 

if ($_REQUEST["to"]<>'') { 
    $criteria['to_date'] = array($_REQUEST['to'], '<='); 
} 

$sql = "SELECT * FROM $SETTINGS['data_table'] WHERE id > 0"; 

foreach ($criteria as $field => $filter) { 

    $comparator = is_array($filter) ? $filter[1] : '='; 
    $value = mysql_real_escape_string(is_array($filter) ? $filter[0] : $filter); 
    $sql .= " AND $field $comparator '$value'"; 

} 

$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)) { 
0

有這個代碼去:

<?php 

// only process if the request is POST 
if (!empty($_POST)) { 
    // prepare the base select 
    $sql = 'SELECT * 
      FROM ' . $SETTINGS['data_table']; 

    // list of where conditions 
    $where = array(); 

    // map of POST => DB fields 
    $maps = array(
     'from'  => 'from_date', 
     'to'  => 'to_date', 
     'city'  => 'city', 
     'full_name' => 'full_name', 
    ); 

    // process all possible POST fields 
    foreach ($maps as $postName => $fieldName) { 
     if (!empty($_POST[$postName])) { 
      $where[] = sprintf('? = "?"', $fieldName, mysql_real_escape_string($_POST[$postName])); 
     } 
    } 

    // append the WHERE statement if required 
    if (!empty($where)) { 
     $sql .= ' WHERE ' . join(' AND ', $where); 
    } 

    // execute and process the query 
    $qry = mysql_query($sql, $connection); 
    if ($qry === false) { 
     throw new Exception(mysql_error(), mysql_errno()); 
    } 

    while ($row = mysql_fetch_assoc($qry)) { 
     // process the row 
    } 
} 
0

終於得到它的工作!我用這個:

$whereClauses = array(); 
if (! empty($_POST['full_name'])) $whereClauses[] ="full_name='".mysql_real_escape_string($_POST['full_name'])."'"; 
if (! empty($_POST['city'])) $whereClauses[] ="city='".mysql_real_escape_string($_POST['city'])."'"; 
$where = ''; 
if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); } 
$sql = mysql_query("SELECT * FROM data".$where); 

我在另一篇文章使用這種試圖放棄了,開始是這樣,放棄了這種方式,又回到第一種方式,得到了它的工作。