2015-02-10 88 views
-2

我在編輯我的搜索欄,以便用戶可以搜索以逗號分隔的值。它調用mySQL數據庫。誰能幫忙?我相信這一定很明顯,但我是新的。謝謝!搜索欄能夠用逗號進行搜索-mySQL/php

搜索頁面:

<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?> 
<?php require($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?> 
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?> 

<?php include("../includes/header-home.php"); ?> 
<div class="container"> 
    <div class="col=md-12"> 
    <p><strong>Search:</strong></p> 
    <form name="form1" method="post" action="search_results.php"> 
    <p><input name="search" type="text" size="40" maxlength="50"/></p> 
    <p> 
    <input type="checkbox" name="type[]" value="1"> Medalist</p> 

<p><strong>Medal:</strong> 
<select name="medal"> 
<option disabled selected></option> 
<option value="1">Medal 1</option> 
<option value="2">Medal 2</option> 
<option value="3">Medal 3</option> 

</select> 
</p> 
<p><strong>State:</strong> 
<select name="state"> 
<option disabled selected></option> 
<option value="1">AL</option> 
<option value="2">AK</option> 
<option value="3">AZ</option> 
<option value="4">AR</option> 

</select> 
</p> 
<p><strong>Year:</strong> 
<select name="year"> 
<option disabled selected></option> 
<option value="2014">2014</option> 
<option value="2013">2013</option> 
<option value="2012">2012</option> 
<option value="2011">2011</optio> 
</select> 
</p> 
<p><strong>Topic:</strong> 
<select name="field"> 
<option disabled selected></option> 
<option value="1">Field 1</option> 
<option value="2">Field 2</option> 
<option value="3">Field 3</option> 

</select> 
</p> 

搜索結果頁:

<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?> 
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?> 
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?> 

<?php 
if (!isset ($_POST['search'])) { 
    header("Location:admin.php"); 
} 

$search = $_POST['search']; 
     $type = (isset($_POST['type']) ? "AND medalist = '1'" : ""); 
     $medal = (isset($_POST['medal']) ? "AND medal = '{$_POST['medal']}'" : ""); 
     $state = (isset($_POST['state']) ? "AND state = '{$_POST['state']}'" : ""); 
     $year = (isset($_POST['year']) ? "AND year = '{$_POST['year']}'" : ""); 
     $field = (isset($_POST['field']) ? "AND field = '{$_POST['field']}'" : ""); 


$search_sqli = " 
      SELECT * FROM profiles WHERE (
        first_name  LIKE '%{$search}%' 
       OR last_name  LIKE '%{$search}%' 
       OR first_name2  LIKE '%{$search}%' 
       OR last_name2  LIKE '%{$search}%' 
       OR last_name2  LIKE '%{$search}%' 
       OR city   LIKE '%{$search}%' 
       OR agency   LIKE '%{$search}%' 
       OR subcomponent LIKE '%{$search}%' 
       OR team_name  LIKE '%{$search}%' 
       OR achievement  LIKE '%{$search}%' 
       OR profile   LIKE '%{$search}%' 
       OR year   LIKE '%{$search}%' 
      ) 
      {$type} 
      {$medal} 
      {$state} 
      {$year} 
      {$field} 
     "; 

$search_query=mysqli_query($connection, $search_sqli); 
if (mysqli_num_rows($search_query) !=0) { 
$search_rs=mysqli_fetch_assoc($search_query); 
} 

?> 

<?php include("../includes/header-home.php"); ?> 
<div class="container"> 
    <div class="col=md-12"> 
    <p>Search:</p> 
    <form name="form1" method="post" action="search_results.php"> 
    <input name="search" type="text" size="40" maxlength="50"/> 
    <input type="submit" name="submit" value="Search" /> 

    </form> 
    <br /> 
    <p><strong>Search Results:</strong></p> 
    <?php if (mysqli_num_rows($search_query) !=0) { 
    do { 
     ?> 
    <p><ul> 
    <li><a href="view_profile.php?profile=<?php echo urlencode($search_rs["id"]); ?>"><?php echo $search_rs['first_name']; ?> <?php echo $search_rs['last_name']; ?> <?php echo $search_rs['first_name2']; ?> <?php echo $search_rs['last_name2']; ?></a></li></ul></p>  

<?php } while ($search_rs=mysqli_fetch_assoc($search_query)); 

    } else { 
     echo "No results found"; 
    } 
    ?> 
    <br />  
    <p> <a class="btn btn-default" href="search.php" role="button">Back to search</a></p> 
    </div></div> 

<?php include($_SERVER['DOCUMENT_ROOT']."/includes/footer.php");?> 
+0

如果你確實有一個用逗號分隔的值的搜索框,你應該在某處使用'explode()',但是我沒有看到它。 – developerwjk 2015-02-10 20:04:36

回答

0

你可以這樣做:

$searchArray = explode(',', $_POST['search']); 

然後通過該數組運行每個項目的搜索查詢,追加每個查詢的結果,然後顯示完整列表。

0

你可以爆炸搜索,然後使用這些值?

$search = $_POST['search']; 
$myNewSearch = explode(",",$search); 
$searchLen = count($myNewSearch); 
for($i = 0; $i < $searchLen; $i++){ 
Select * from profiles WHERE (
first_name  LIKE '%{$myNewSearch[$i]}%' 
... 
} 

它有點長,但它會讓你在沒有逗號的情況下輸入所有內容。