2017-09-13 512 views
0

好吧,讓我儘可能具體....我想改變我的搜索欄的行爲方式。現在,字符串必須完全匹配MySQL查詢!或者它不會顯示我的產品。PHP拆分搜索欄關鍵字按空格鍵

這裏是我的了: 一種 「案例iphone」

searchitems.php?tosearch=case+iphone&Search=Search 

enter image description here

現在搜索 「iPhone外殼」

searchitems.php?tosearch=iphone+case&Search=Search 

enter image description here

搜索現在基於查詢如何工作...如果我在URL手動這種類型的...它的作品我如何想:

searchitems.php?tosearch=%iphone%&%case%&Search=Search 

enter image description here

那麼,如何去從

searchitems.php?tosearch=case+iphone&Search=Search 

網址更改爲

searchitems.php?tosearch=%iphone%&%case%&Search=Search 

這是我到目前爲止的代碼:

搜索表單在index.php文件

<form method="GET" action="searchitems.php"> 
    <input size="50" type="text" name="tosearch" value="Search" name="keyword" id="keyword" title="keyword" onfocus="clearText(this)" onblur="clearText(this)" class="txt_field"> 
    <input type="submit" name="Search" value="Search" alt="Search" id="searchbutton" title="Search" class="sub_btn"> 
</form> 

searchitems.php(很抱歉,如果它沒有正確標籤只是複製和粘貼)

<?php 
include('core/header2.php'); 
include ('core/connectdb.php'); 
if(isset($_GET['tosearch'])) { 
$tosearch=$_GET['tosearch']; 
$tosearch=urldecode($tosearch); 
$tosearch = preg_replace('!\s+!', ' ', trim($tosearch)); 
$search_terms = explode(' ',$tosearch); 
$search_terms[] = $tosearch; 
$search_terms=array_unique($search_terms); 
$query = "select * from products where "; 
$query_fields = Array(); 
$sql = "SHOW COLUMNS FROM products"; 
$columnlist = $connect->query($sql); 
while($arr = $columnlist->fetch_assoc()){ 
    extract($arr); 
    $query_fields[] = $Field . " LIKE ('%". $tosearch . "%')"; 
} 
$query .= implode(" OR ", $query_fields); 
       $results = mysqli_query($connect, $query) or die(mysql_error()); 
       $rows = $results->num_rows; 
       if ($rows > 0) { 
        $cols = 5; 
        $counter = 1; 
        $nbsp = $cols - ($rows % $cols); 
        echo '<div id="content" class="float_r">'; 
        echo "<table border=\"0\">"; 
        while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) { 
         if(($counter % $cols) == 1) { // Check if it's new row 
          echo '<tr align="center">'; 
         } 
         extract($row); 
         echo '<td valign="top" style="padding-right:15px;">'; 
         echo "<a href=itemdetails.php?itemcode=$item_code>"; 
         echo '<img src=' . $imagename . ' style="max-width:120px;max-height:140px; 
         width:auto;height:auto;"></img><br/>'; 
         echo $item_name .'<br/>'; 
         echo "</a>"; 
         echo '<div class="product_price">$'. $price .'</div>'; 
         echo "<form method=\"POST\" action=\"cart.php?action=add&icode=$item_code&iname=$item_name&iprice=$price&ilocation=$location\">"; 
         echo "<input type=\"submit\" name=\"addtocart\" value=\"Add To Cart\"></form>"; 
         echo "</td>"; 
         if(($counter % $cols) == 0){ 
          echo "</tr>"; 
         } 
         $counter++; 
        } 
        if($nbsp > 0) { // Add unused column in last row 
         for ($i = 0; $i < $nbsp; $i++) { 
          echo '<td>&nbsp;</td>';  
         } 
         echo '</tr>'; 
        } 

           } 
} 
echo '</table></div><div class="cleaner"></div>'; 
include('core/footer.php'); 
?> 

回答

0

好,大哥,這是一個不錯的辦法如何正在做。但爲了學習,它很好。對於+標誌的問題,你可以使用PHP urldecode,例如:

<?php 
$tosearch = 'a+b'; 
echo urldecode($tosearch); 

它有自己的親/ CON的事情,但在高級別它會爲你工作,你可以挖掘更多的這個問題,如果你喜歡。

+0

+符號只是因爲$ tosearch變量已經在它們中有空格urldecode();只是拿出任何+字符......這不能解決我的問題,如何替換空格,並將其輸入到我的網址%item%&%name%。 –

+0

爲什麼在搜索字詞中需要'%'符號?我爲您創建了一個要點並運行示例代碼。看一看。 https://gist.github.com/mszb/d2b6fd566ed5e390e98bf419bcd944ec - http://main.xfiddle.com/code_33641226.php?s=iphone+case –

+0

我更新了我的搜索代碼,與您提供的內容一起工作...不幸的是,當我搜索「case iphone」時,它仍然給了我相同的結果我得到了一個結果,但「iphone case」沒有結果。我需要我的搜索將兩個關鍵字分開,以便搜索「case」的任何結果以及任何帶有「iphone」的結果。將網址中的%iphone%&%case%設置爲像我希望的那樣。 –