2015-05-04 39 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