使用选项编写PHP/MYSQL搜索代码的最佳方法

使用选项编写PHP/MYSQL搜索代码的最佳方法,php,mysql,forms,search,filtering,Php,Mysql,Forms,Search,Filtering,因此,我正在构建一个搜索表单,它有很多选项供用户选择。从下图中可以看到,用户选择了搜索条件,并允许他们输入或检查自己喜欢的内容。如果取消勾选,则删除所有值/取消勾选所有框 我最初只搜索了体重/身高/性别,但从那时起,我添加了更多 我已经对weight/height/gender搜索选项进行了编码,结果是有很多if语句检查所选内容/null,然后创建相应的MYSQL查询 我不完全确定我应该如何处理其余的选项(如果我应该重新开始的话)。有没有更容易的办法?我只是需要一些指导,这样我可以使这更有效一点

因此,我正在构建一个搜索表单,它有很多选项供用户选择。从下图中可以看到,用户选择了搜索条件,并允许他们输入或检查自己喜欢的内容。如果取消勾选,则删除所有值/取消勾选所有框

我最初只搜索了体重/身高/性别,但从那时起,我添加了更多

我已经对weight/height/gender搜索选项进行了编码,结果是有很多if语句检查所选内容/null,然后创建相应的MYSQL查询

我不完全确定我应该如何处理其余的选项(如果我应该重新开始的话)。有没有更容易的办法?我只是需要一些指导,这样我可以使这更有效一点

谢谢

require'functions.php';
//搜索数据
//$weight\u min=$\u POST['weight\u min'];
//$weight\u max=$\u POST['weight\u max'];
//$height\u min=$\u POST['height\u min'];	
//$height\u max=$\u POST['height\u max'];
//$gender\u select=$\u POST['gender\u select'];
如果(“==修剪($\u POST['weight\u min'])){
$weight_min='';
}
其他的
{
$weight\u min=$\u POST['weight\u min'];
}
如果(“==修剪($\u POST['weight\u max'])){
$weight_max='';
}
其他的
{
$weight\u max=$\u POST['weight\u max'];
}
如果(“==修剪($\u POST['height\u min'])){
$height_min='';
}
其他的
{
$height\u min=$\u POST['height\u min'];
}
如果(“==修剪($\u POST['height\u max'])){
$height_max='';
}
其他的
{
$height\u max=$\u POST['height\u max'];
}
如果(!isset($\u POST['gender\u select'])){
$gender_select='';
}
其他的
{
$gender\u select=$\u POST['gender\u select'];
}
//显示测试
//echo“发送:最小重量:”.$weight\u min.“最大重量:”.$weight\u max.“最小高度:”.$height\u min.“最大高度:”.$height\u max.“性别选择:”.$gender\u select.”;
检查空值($weight\u min、$weight\u max、$height\u min、$height\u max、$gender\u select);
我用这种方式:

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))
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)
从表中选择*
哪里

((weight>=:weightMin和weight=:weightMin和:weightMax为空)或(weight以下是用于动态构建查询的常用方法的伪代码:

$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
$qry='SELECT*FROM character_information WHERE';
//如果用户输入了最大高度标准
如果($最大高度){
$qry.='字符重量=:最小高度';
}否则{
$qry.='1=1';
}
$qry.='和';
//如果用户输入了车身构造标准
如果($body_build){
$qry.='char_body_build=:body_build';
}否则{
$qry.='1=1';
}
$qry.='和';
...
//在此处准备并绑定参数

你能发布一个简短的代码示例来说明你当前的方法吗?你可以在文章中循环或获取值,并动态构建你的查询。一定要将用户值从查询中分离出来。发布了我的一些代码!是的,我正在考虑做循环。我目前做的方式一点也不好,你可能想要定义e“过滤器”-类,使用选项being objects。这样,您可以简单地清理输入,并将对象数组作为函数参数。我不明白您为什么要在任何地方对此进行循环。要让MySQL正确地评估它,并允许它利用标准上的索引而不是扫描,您需要通过添加
IS NOT NULL
来扩展它,如:
(:weightMin为空且:weightMax为空)或(:weightMin不为空且:weightMax为空且重量>=:weightMin)或…
$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