因此,我構建了一個搜索表單,其中有很多選項供用戶選擇。正如您從下面的圖片中看到的,用戶選擇一個搜索條件,並允許他們輸入或查看他們喜歡的內容。如果未勾選,則刪除所有值/取消選中所有框。使用選項編碼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