使用选项编码 PHP/MYSQL 搜索的最佳方法 [关闭]
Posted
技术标签:
【中文标题】使用选项编码 PHP/MYSQL 搜索的最佳方法 [关闭]【英文标题】:Best way to code PHP/MYSQL search with options [closed] 【发布时间】:2015-07-13 20:28:40 【问题描述】:所以我正在构建一个搜索表单,它有很多选项供用户选择。正如您从下图中看到的那样,用户选择了搜索条件,并允许他们输入或检查他们喜欢的内容。如果未选中此项,则会删除所有值/取消选中所有框。
我最初只有体重/身高/性别作为搜索,但从那以后增加了更多。
我已经对体重/身高/性别搜索选项进行了编码,结果是大量的 if 语句检查选择/空的内容,然后创建适当的 mysql 查询。
我不完全确定我应该如何处理其他选项(如果我应该重新开始)。有没有更容易的方法?我只是需要一些指导,这样我才能使它更有效。
谢谢!
require 'functions.php';
//Search data
//$weight_min = $_POST['weight_min'];
//$weight_max = $_POST['weight_max'];
//$height_min = $_POST['height_min'];
//$height_max = $_POST['height_max'];
//$gender_select = $_POST['gender_select'];
if("" == trim($_POST['weight_min']))
$weight_min = '';
else
$weight_min = $_POST['weight_min'];
if("" == trim($_POST['weight_max']))
$weight_max = '';
else
$weight_max = $_POST['weight_max'];
if("" == trim($_POST['height_min']))
$height_min = '';
else
$height_min = $_POST['height_min'];
if("" == trim($_POST['height_max']))
$height_max = '';
else
$height_max = $_POST['height_max'];
if (!isset($_POST['gender_select']))
$gender_select = '';
else
$gender_select = $_POST['gender_select'];
//Show test
//echo "sent: weight-min: " .$weight_min. " weight-max: " .$weight_max. " height-min: ".$height_min." height-max: ".$height_max." gender-select: ".$gender_select."<p>";
check_null_values($weight_min, $weight_max, $height_min, $height_max, $gender_select);
function check_null_values($weight_min, $weight_max, $height_min, $height_max, $gender_select)
//Weight
if($weight_min !=null && $weight_max != null && $height_min == null && $height_max == null && $gender_select == null)
select_weight($weight_min, $weight_max);
//echo "select_weight";
//Height
else if($weight_min == null && $weight_max == null && $height_min != null && $height_max != null && $gender_select == null)
select_height($height_min, $height_max);
//echo "select_height";
//Gender
else if($weight_min == null && $weight_max == null && $height_min == null && $height_max == null && $gender_select != null)
select_gender($gender_select);
//echo "select_gender";
//Weight + Height
else if($weight_min != null && $weight_max != null && $height_min != null && $height_max != null && $gender_select == null)
select_weight_height($weight_min, $weight_max, $height_min, $height_max);
//echo "select_weight_height";
//Weight + Gender
else if($weight_min != null && $weight_max != null && $height_min == null && $height_max == null && $gender_select != null)
select_weight_gender($weight_min, $weight_max, $gender_select);
//echo "select_weight_gender";
//Height + Gender
else if($weight_min == null && $weight_max == null && $height_min != null && $height_max != null && $gender_select != null)
select_height_gender($height_min, $height_max, $gender_select);
//echo "select_height_gender";
//All
else if($weight_min != null && $weight_max != null && $height_min != null && $height_max != null && $gender_select != null)
select_all($weight_min, $weight_max, $height_min, $height_max, $gender_select);
//echo "select_all";
else if($weight_min == null && $weight_max == null && $height_min == null && $height_max == null && $gender_select == null)
select_none();
//echo "select_none";
else
//echo "Please enter missing parameter";
//Weight only selected
function select_weight($weight_min, $weight_max)
include 'db_connect.php';
$result = mysqli_query($db,
"SELECT * FROM character_information
WHERE
(char_min_weight BETWEEN '".$weight_min."' AND '" .$weight_max."'
OR char_max_weight BETWEEN '".$weight_min."' AND '" .$weight_max."')
OR
('".$weight_min."' BETWEEN char_min_weight AND char_max_weight
OR '" .$weight_max."' BETWEEN char_min_weight AND char_max_weight)
");
return get_result($result);
//Height only selected
function select_height($height_min, $height_max)
include 'db_connect.php';
$result = mysqli_query($db,
"SELECT * FROM character_information
WHERE
(char_min_height BETWEEN '".$height_min."' AND '" .$height_max."'
OR char_max_height BETWEEN '".$height_min."' AND '" .$height_max."')
OR
('".$height_min."' BETWEEN char_min_height AND char_max_height
OR '" .$height_max."' BETWEEN char_min_height AND char_max_height)
");
get_result($result);
//Gender only selected
function select_gender($gender_select)
include 'db_connect.php';
$result = mysqli_query($db,
"SELECT * FROM character_information
WHERE char_gender = '".$gender_select."'
");
get_result($result);
//Weight + Height selected
function select_weight_height($weight_min, $weight_max, $height_min, $height_max)
include 'db_connect.php';
$result = mysqli_query($db,
"SELECT * FROM character_information
WHERE
((char_min_weight BETWEEN '".$weight_min."' AND '" .$weight_max."'
OR char_max_weight BETWEEN '".$weight_min."' AND '" .$weight_max."')
OR
('".$weight_min."' BETWEEN char_min_weight AND char_max_weight
OR '" .$weight_max."' BETWEEN char_min_weight AND char_max_weight))
AND
((char_min_height BETWEEN '".$height_min."' AND '" .$height_max."'
OR char_max_height BETWEEN '".$height_min."' AND '" .$height_max."')
OR
('".$height_min."' BETWEEN char_min_height AND char_max_height
OR '" .$height_max."' BETWEEN char_min_height AND char_max_height))
");
get_result($result);
【问题讨论】:
您能否发布一个简短的代码示例来说明您当前的方法? 您可以遍历 POST 或 GET 值并动态构建您的查询。请务必从查询中分离出用户值。 贴出我的一些代码!是的,我正在考虑做循环。我目前所做的方式一点都不好 您可能想要定义一个“过滤器”类,其中包含选项蜂对象。这样您就可以简单地清理输入,并将对象数组作为函数参数。 我不明白你为什么要为此循环到任何地方。 【参考方案1】:这是动态构建查询的常用方法的伪代码:
$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
【讨论】:
【参考方案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)
【讨论】:
要让 MySQL 正确评估它并允许它利用标准上的索引而不是扫描,您需要通过添加IS NOT NULL
来扩展它,例如:((:weightMin IS NULL AND :weightMax IS NULL) OR (:weightMin IS NOT NULL AND :weightMax IS NULL AND weight >= :weightMin) OR ...
。以上是关于使用选项编码 PHP/MYSQL 搜索的最佳方法 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章