2016-03-04 122 views
-2

所以基本上我試圖讓搜索採取標題,然後在搜索中忽略標題,但採取關鍵字與該標題相關聯,並顯示其他項目具有相同的關鍵字,如果這是有道理的。PHP搜索查詢不起作用

這是我的代碼,但它只搜索你輸入的內容。我不知道如何讓它做我想要的。

<?php 
     $k = $_GET['lookfor']; 
     $terms = explode(" ",$k); 
     $query = "SELECT * FROM search WHERE "; 
     foreach ($terms as $each) { 
      $i++; 
      if ($i == 1) 
       $query .= "keywords LIKE '%$each%' "; 
      else  
       $query .= "OR keywords LIKE '%$each%' "; 
     } 
    // connect 
    mysql_connect("localhost", "root", "password"); 
    mysql_select_db("icarus"); 

    $query = mysql_query($query); 
    $numrows = mysql_num_rows($query); 
    if ($numrows > 0) { 

     while ($row = mysql_fetch_assoc($query)){ 
      $id = $row['id']; 
      $title = $row['title']; 
      $description = $row['description']; 
      $keywords = $row['keywords']; 
      $link = $row['link']; 

      echo "<li type='1'><h3><a href='$link'>$title</a></h3> 
      <p>$description</p><br /><br /></li>"; 
     } 
    } 
    else 
     echo "<p>No results found for '<b>$k</b>'. Icarus is still in the early development stage, this can happen.</p>"; 

    // disconnect 
    mysql_close(); 
    ?>  </div> 

的樣本數據:

ID:   INT 
Title :  Linkin Park - Hands Held High 
Description: About the song 
Keywords: rap rock anti war political 
Link:  youtube link to song 
+1

你是什麼意思'拿冠軍的意思,然後在搜索忽略標題而是採取關聯關鍵字' – urfusion

+0

請添加表格定義和一些示例數據 –

+0

我的意思是不顯示與搜索關鍵字相似的標題,但只顯示具有相似關鍵字的標題 –

回答