带有选项的编码PHP/MYSQL搜索的最佳方法 [英] Best way to code PHP/MYSQL search with options
问题描述
因此,我正在构建一个搜索表单,其中有很多选项供用户选择.如您从下面的图像中看到的,用户选择一个搜索条件,并允许他们输入或查看他们喜欢的内容.如果未选中,则会删除所有值/取消选中所有复选框.
So I am building a search form which has a lot of options for users to select from. As you can see from the image below a user selects a search criteria and it allows them to enter or check what they like. If this is unticked it removes all values/unchecks all the boxes.
我最初只搜索体重/身高/性别,但此后又添加了更多内容.
I originally only had weight/height/gender as the search but since then have added more.
我已经对体重/身高/性别搜索选项进行了编码,结果结束了很多if语句检查了选择的内容/是否为空,然后创建了相应的MYSQL查询.
I already coded the weight/height/gender search options and it ended being a lot of if statements checking what was selected/null then creating the appropriate MYSQL queries.
对于其他选项,我不确定如何继续(如果我应该重新开始).有没有更容易解决的办法?我只需要一些指导,这样我就可以使它更有效.
I am not entirely sure how I should go about (if I should start again) with the rest of the options. Is there an easier away around this? I just need some direction so I can make this a bit more effective.
谢谢!
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);
}
推荐答案
我正在使用这种方式:
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<时,这相当长.过滤器< y
This is pretty long when it is x < filter < y
否则,如果只是性别"之类的一种类型:
Else if this is only one type like 'Gender' :
if(empty($_GET['gender'])) {
$gender = null;
} else $gender = $_GET['gender'];
SQL:
SELECT * FROM TABLE
WHERE (gender = :gender or :gender is null)
如果选择了性别,它将搜索出正确的性别,否则返回true,并且不会影响您的陈述.
If gender is selected, it will search the good one, else it returns true and doesn't impact your statement.
组合查询:
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)
这篇关于带有选项的编码PHP/MYSQL搜索的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!