2015-06-21 103 views
1

我遇到的問題是當搜索詞「反」時它會顯示產品,但是當基於搜索的單詞「反皺紋」沒有提到搜索結果時。請參閱下面的代碼和建議,我錯過了什麼。搜索功能對某些單詞不起作用

$search_output = ''; 

if(isset($_POST['search'])) { 
    $searchsql = $_POST['search']; 
    $searchsql = preg_replace('#[^a-z [email protected]]#i', '', $searchsql); 

    $query = mysql_query("SELECT tb_spa_prd.*, tb_spa_prd_cat.spa_prd_cat FROM tb_spa_prd AS tb_spa_prd INNER JOIN tb_spa_prd_cat ON tb_spa_prd.spa_prd_cat_id = tb_spa_prd_cat.spa_prd_cat_id WHERE spa_prd_cat LIKE '%$searchsql%' or spa_prd_code LIKE '%$searchsql%' or spa_prd_name LIKE '%$searchsql%'") or die ("Could no search!"); 
    $count = mysql_num_rows($query); 
    if($count == 0) { 
     $search_output .= '<tr> 
          <td colspan="9" style="text-align: center">There was no search results!</td> 
          </tr>'; 
    } else { 
     $row_no = 1; 
     while($row = mysql_fetch_array($query,MYSQL_ASSOC)) { 
      $spa_prd_id = $row["spa_prd_id"]; 
      $spa_prd_cat = $row["spa_prd_cat"]; 
      $spa_prd_code = $row["spa_prd_code"]; 
      $spa_prd_name = $row["spa_prd_name"]; 
      $spa_prd_vlm = $row["spa_prd_vlm"]; 
      $spa_prd_qty = $row["spa_prd_qty"]; 
      $spa_prd_crt_date = $row["spa_prd_crt_date"]; 
      $spa_prd_crt_usr = $row["spa_prd_crt_usr"]; 
      $search_output .= '<tr> 
            <td>'.$row_no++.'</td> 
            <td>'.$spa_prd_cat.'</td> 
            <td>'.$spa_prd_code.'</td> 
            <td>'.$spa_prd_name.'</td> 
            <td>'.$spa_prd_vlm.'</td> 
            <td>'.$spa_prd_qty.'</td> 
            <td>'.$spa_prd_crt_date.'</td> 
            <td>'.$spa_prd_crt_usr.'</td> 
          <td><a href=\'edit_spa_prd.php?spa_prd_id='.$spa_prd_id.'\'>Edit</a> | <a href=\'delete_spa_prd.php?spa_prd_id='.$spa_prd_id.'\'>Delete</a></td> 
           </tr>'; 
     } // close while loop 
    } 
} 
+1

發佈所有的代碼代替,最好是隻包括所需要的部分。您應打印查詢並將其運行到您的phpmyadmin中以查看結果或調試您自己 –

+0

我建議您將全文索引用於搜索中使用的列。 – Allkin

+0

@Quinn如果你想''反皺紋「'觸發你將需要包括一個單獨的'LIKE'謂詞爲_each_ term,q.v.我的迴應如下。 –

回答

0

你可以嘗試明確包括LIKE謂詞每個搜索詞是這樣的:

$searchsql = $_POST['search']; 
$terms = explode(" ", $searchsql); 
$likecols = array("spa_prd_cat", "spa_prd_code, "spa_prd_name"); 
$likesql = ""; 
foreach ($likecols as $likecol) { 
    for ($i = 0; $i < count($terms); ++$i) { 
     if ($likecol == "spa_prd_cat" && $i == 0) { 
      $likesql = "spa_prd_cat LIKE $terms[0]"; 
     } 
     else { 
      $likesql .= " OR $likecol LIKE $terms[$i]"; 
     } 
    } 
} 

$querystr = "SELECT tb_spa_prd.*, tb_spa_prd_cat.spa_prd_cat FROM tb_spa_prd AS tb_spa_prd INNER JOIN tb_spa_prd_cat ON tb_spa_prd.spa_prd_cat_id = tb_spa_prd_cat.spa_prd_cat_id WHERE "; 
$querystr .= $likesql; 
$query = mysql_query($querystr) or die ("Could no search!"); 
+0

對不起。我對這很新鮮。我試過你提供的代碼,但提到無法搜索。以及我真的不知道'我'是指。請指教。 – Quinn

+0

如果你想搜索''抗皺紋''你可以使用'LIKE'反'或'像'皺紋'。我的代碼試圖做到這一點。 –

相關問題