2015-05-04 69 views
0

因此,我構建了一個搜索表單,其中有很多選項供用戶選擇。正如您從下面的圖片中看到的,用戶選擇一個搜索條件,並允許他們輸入或查看他們喜歡的內容。如果未勾選,則刪除所有值/取消選中所有框。使用選項編碼PHP/MYSQL搜索的最佳方式

我原本只有體重/身高/性別作爲搜索但自那時以來已經增加了更多。

我已編碼的體重/身高/性別搜索選項,它結束了被大量的if語句檢查什麼選擇/ null,則創建適當MYSQL查詢。

我不完全相信,我應該怎麼去(如果我要重新開始)與其餘的選項。圍繞這個更容易嗎?我只是需要一些方向,所以我可以使這一點更有效。

謝謝!

enter image description here

enter image description here

require 'functions.php'; 
 
\t 
 
\t //Search data 
 
\t //$weight_min = $_POST['weight_min']; 
 
\t //$weight_max = $_POST['weight_max']; 
 
\t //$height_min = $_POST['height_min']; \t 
 
\t //$height_max = $_POST['height_max']; 
 
\t //$gender_select = $_POST['gender_select']; 
 
\t 
 
\t if("" == trim($_POST['weight_min'])){ 
 
    \t $weight_min = ''; 
 
\t } 
 
\t else 
 
\t { 
 
\t \t $weight_min = $_POST['weight_min']; 
 
\t } 
 
\t 
 
\t if("" == trim($_POST['weight_max'])){ 
 
    \t $weight_max = ''; 
 
\t } 
 
\t else 
 
\t { 
 
\t \t $weight_max = $_POST['weight_max']; 
 
\t } 
 
\t 
 
\t if("" == trim($_POST['height_min'])){ 
 
    \t $height_min = ''; 
 
\t } 
 
\t else 
 
\t { 
 
\t \t $height_min = $_POST['height_min']; 
 
\t } 
 
\t 
 
\t if("" == trim($_POST['height_max'])){ 
 
    \t $height_max = ''; 
 
\t } 
 
\t else 
 
\t { 
 
\t \t $height_max = $_POST['height_max']; 
 
\t } 
 
\t 
 
\t if (!isset($_POST['gender_select'])){ 
 
    \t 
 
\t \t $gender_select = ''; 
 
\t } 
 
\t else 
 
\t { 
 
\t \t $gender_select = $_POST['gender_select']; 
 
\t } 
 
\t 
 
\t //Show test 
 
\t //echo "sent: weight-min: " .$weight_min. " weight-max: " .$weight_max. " height-min: ".$height_min." height-max: ".$height_max." gender-select: ".$gender_select."<p>"; 
 
\t 
 
\t check_null_values($weight_min, $weight_max, $height_min, $height_max, $gender_select); 
 
\t

\t function check_null_values($weight_min, $weight_max, $height_min, $height_max, $gender_select) 
 
\t { 
 
\t \t //Weight 
 
\t \t if($weight_min !=null && $weight_max != null && $height_min == null && $height_max == null && $gender_select == null) 
 
\t \t { 
 
\t \t \t select_weight($weight_min, $weight_max); 
 
\t \t \t //echo "select_weight"; 
 
\t \t } 
 
\t \t //Height 
 
\t \t else if($weight_min == null && $weight_max == null && $height_min != null && $height_max != null && $gender_select == null) 
 
\t \t { 
 
\t \t \t select_height($height_min, $height_max); 
 
\t \t \t //echo "select_height"; 
 
\t \t } \t 
 
\t \t //Gender 
 
\t \t else if($weight_min == null && $weight_max == null && $height_min == null && $height_max == null && $gender_select != null) 
 
\t \t { 
 
\t \t \t select_gender($gender_select); 
 
\t \t \t //echo "select_gender"; 
 
\t \t } 
 
\t \t //Weight + Height 
 
\t \t else if($weight_min != null && $weight_max != null && $height_min != null && $height_max != null && $gender_select == null) 
 
\t \t { 
 
\t \t \t select_weight_height($weight_min, $weight_max, $height_min, $height_max); 
 
\t \t \t //echo "select_weight_height"; 
 
\t \t } 
 
\t \t //Weight + Gender 
 
\t \t else if($weight_min != null && $weight_max != null && $height_min == null && $height_max == null && $gender_select != null) 
 
\t \t { 
 
\t \t \t select_weight_gender($weight_min, $weight_max, $gender_select); 
 
\t \t \t //echo "select_weight_gender"; 
 
\t \t } 
 
\t \t //Height + Gender 
 
\t \t else if($weight_min == null && $weight_max == null && $height_min != null && $height_max != null && $gender_select != null) 
 
\t \t { 
 
\t \t \t select_height_gender($height_min, $height_max, $gender_select); 
 
\t \t \t //echo "select_height_gender"; 
 
\t \t } 
 
\t \t //All 
 
\t \t else if($weight_min != null && $weight_max != null && $height_min != null && $height_max != null && $gender_select != null) 
 
\t \t { 
 
\t \t \t select_all($weight_min, $weight_max, $height_min, $height_max, $gender_select); 
 
\t \t \t //echo "select_all"; 
 
\t \t } 
 
\t \t else if($weight_min == null && $weight_max == null && $height_min == null && $height_max == null && $gender_select == null) 
 
\t \t { 
 
\t \t \t select_none(); 
 
\t \t \t //echo "select_none"; 
 
\t \t } 
 
\t \t else 
 
\t \t { 
 
\t \t \t //echo "Please enter missing parameter"; 
 
\t \t } 
 
\t 
 
\t 
 
\t }

//Weight only selected 
 
\t function select_weight($weight_min, $weight_max) 
 
\t { 
 
\t \t include 'db_connect.php'; 
 
\t \t $result = mysqli_query($db, 
 
\t \t "SELECT * FROM character_information 
 
\t \t WHERE 
 
\t \t (char_min_weight BETWEEN '".$weight_min."' AND '" .$weight_max."' 
 
\t \t OR char_max_weight BETWEEN '".$weight_min."' AND '" .$weight_max."') 
 
\t \t OR 
 
\t \t ('".$weight_min."' BETWEEN char_min_weight AND char_max_weight 
 
\t \t OR '" .$weight_max."' BETWEEN char_min_weight AND char_max_weight) 
 
\t \t "); 
 
\t \t 
 
\t \t return get_result($result); 
 
\t } 
 
\t 
 
\t //Height only selected 
 
\t function select_height($height_min, $height_max) 
 
\t { 
 
\t \t include 'db_connect.php'; 
 
\t \t $result = mysqli_query($db, 
 
\t \t "SELECT * FROM character_information 
 
\t \t WHERE 
 
\t \t (char_min_height BETWEEN '".$height_min."' AND '" .$height_max."' 
 
\t \t OR char_max_height BETWEEN '".$height_min."' AND '" .$height_max."') 
 
\t \t OR 
 
\t \t ('".$height_min."' BETWEEN char_min_height AND char_max_height 
 
\t \t OR '" .$height_max."' BETWEEN char_min_height AND char_max_height) 
 
\t \t "); 
 
\t \t 
 
\t \t get_result($result); 
 
\t } 
 
\t 
 
\t //Gender only selected 
 
\t function select_gender($gender_select) 
 
\t { 
 
\t \t include 'db_connect.php'; 
 
\t \t 
 
\t \t $result = mysqli_query($db, 
 
\t \t "SELECT * FROM character_information 
 
\t \t WHERE char_gender = '".$gender_select."' 
 
\t \t "); \t 
 
\t \t 
 
\t \t get_result($result); \t 
 
\t \t 
 
\t } 
 
\t 
 
\t //Weight + Height selected 
 
\t function select_weight_height($weight_min, $weight_max, $height_min, $height_max) 
 
\t { 
 
\t \t include 'db_connect.php'; 
 
\t \t 
 
\t \t $result = mysqli_query($db, 
 
\t \t "SELECT * FROM character_information 
 
\t \t WHERE 
 
\t \t ((char_min_weight BETWEEN '".$weight_min."' AND '" .$weight_max."' 
 
\t \t OR char_max_weight BETWEEN '".$weight_min."' AND '" .$weight_max."') 
 
\t \t OR 
 
\t \t ('".$weight_min."' BETWEEN char_min_weight AND char_max_weight 
 
\t \t OR '" .$weight_max."' BETWEEN char_min_weight AND char_max_weight)) 
 
\t \t AND 
 
\t \t ((char_min_height BETWEEN '".$height_min."' AND '" .$height_max."' 
 
\t \t OR char_max_height BETWEEN '".$height_min."' AND '" .$height_max."') 
 
\t \t OR 
 
\t \t ('".$height_min."' BETWEEN char_min_height AND char_max_height 
 
\t \t OR '" .$height_max."' BETWEEN char_min_height AND char_max_height)) 
 
\t \t "); \t 
 
\t \t 
 
\t \t get_result($result); 
 
\t }

+0

你能後的代碼很短的例子來說明你目前的做法? – ToddB

+0

您可以遍歷POST或GET值並動態構建查詢。確保從查詢中分離出用戶值。 – chris85

+0

發表我的一些代碼!是的,我正在考慮做循環。我現在做的方式並不是很好 – Lizzeiy

回答

2

我使用這種方式:

if(empty($_GET['weightMin'])) { 
    $weightMin= null; 
} else $weightMin= $_GET['weightMin']; 

if(empty($_GET['weightMax'])) { 
    $weightMax= null; 
} else $weightMin= $_GET['weightMax']; 

和聲明將是:

SELECT * FROM TABLE 
WHERE ((weight >= :weighttMin AND weight <= :weightMax) OR (weight >= :weightMin AND :weightMax is null) OR (weight <= :weightMax AND :weightMin is null) OR (:weightMax is null AND :weightMin is null)) 

這是相當長的,當它爲x <過濾<ÿ

否則,如果這只是一種類型,如'性別':

if(empty($_GET['gender'])) { 
    $gender = null; 
} else $gender = $_GET['gender']; 

的SQL:

SELECT * FROM TABLE 
WHERE (gender = :gender or :gender is null) 

如果選擇性別,它會搜索好的,否則返回true,不會影響你的發言。

合併查詢:

SELECT * FROM TABLE 
WHERE 
((weight >= :weighttMin AND weight <= :weightMax) OR (weight >= :weightMin AND :weightMax is null) OR (weight <= :weightMax AND :weightMin is null) OR (:weightMax is null AND :weightMin is null)) 
AND 
(gender = :gender or :gender is null) 
+0

爲了讓MySQL正確評估它,並允許它在條件中使用索引而不是掃描,需要通過添加'IS NOT NULL'來擴展這一點:'((::weightMin IS NULL AND:weightMax IS NULL) OR(:weightMin IS NOT NULL AND:weightMax IS NULL AND weight> =:weightMin)或...'。 –

0

下面是僞代碼來構建動態查詢的常用方法:

$qry = 'SELECT * FROM character_information WHERE '; 
// If the user entered a max height criteria 
if ($max_height) { 
    $qry .= 'char_weight <= :max_height'; 
} else { 
    $qry .= '1 = 1'; 
} 
$qry .= ' AND '; 
// If the user entered a min height criteria 
if ($min_height) { 
    $qry .= 'char_weight >= :min_height'; 
} else { 
    $qry .= '1 = 1'; 
} 
$qry .= ' AND '; 
// If the user entered a body build criteria 
if ($body_build) { 
    $qry .= 'char_body_build = :body_build'; 
} else { 
    $qry .= '1 = 1'; 
} 
$qry .= ' AND '; 
... 
// Prepare and bind params here