因此,我构建了一个搜索表单,其中有很多选项供用户选择。正如您从下面的图片中看到的,用户选择一个搜索条件,并允许他们输入或查看他们喜欢的内容。如果未勾选,则删除所有值/取消选中所有框。使用选项编码PHP/MYSQL搜索的最佳方式
我原本只有体重/身高/性别作为搜索但自那时以来已经增加了更多。
我已编码的体重/身高/性别搜索选项,它结束了被大量的if语句检查什么选择/ null,则创建适当MYSQL查询。
我不完全相信,我应该怎么去(如果我要重新开始)与其余的选项。围绕这个更容易吗?我只是需要一些方向,所以我可以使这一点更有效。
谢谢!
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 }
你能后的代码很短的例子来说明你目前的做法? – ToddB
您可以遍历POST或GET值并动态构建查询。确保从查询中分离出用户值。 – chris85
发表我的一些代码!是的,我正在考虑做循环。我现在做的方式并不是很好 – Lizzeiy