2012-07-09 63 views
1

我有以下代碼,但此代碼僅適用於數據庫中的精確匹配,我想要的是例如您要搜索「約翰瓊斯」,如果您鍵入只是joh他姓名和其他同名用戶將出現在下拉列表中,以及姓氏名稱。 我嘗試了一些與'LIKE'功能,但我似乎無法弄清楚。 謝謝!包括下拉搜索與可能的搜索名稱

<?php 
    include ('core/init.php'); 

    logged_in_redirect(); 

    if(empty($_POST['friend_search'])) { 
     header('Location: friends.php'); 
    } 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 

     <title> My Friends</title> 
     <link href='css.css' rel='stylesheet' type='text/css'/> 
    </head> 
    <body class='home-body'> 
     <?php include_once("core/analyticstrackingcode.php") ?> 
     <div class='whitebg'> 
      <?php include('core/navbar.php'); ?> 
      <?php 
       $friend = $_POST['friend_search']; 
       $found=0; 
       $search = mysql_query("SELECT * FROM users"); 
       while($row = mysql_fetch_array($search)) { 
        $possible_match_name = $row['first_name']; 
        $possible_match_id = $row['id']; 
        $possible_match_email = $row['email']; 
        $possible_match_profilepic = $row['profilepic']; 
        $this_users_email = $_SESSION['email']; 
        if($possible_match_email!=$this_users_email) { 
         if($friend==$possible_match_email) { 
          echo " 
           <html> 
            <div class='friendsearchentries'> 
             <img src='$possible_match_profilepic' class='friend-match-prof-pic' /> 
             <a href='confirm_send.php?id=$possible_match_id' class='friend-match-link' ><center>".$possible_match_name."</center></a> 
            </div> 
           </html>"; 
           $found=1; 
         } 
        } 
       } 
       if($found==0) { 
        echo "Sorry, we found no matches."; 
       } 
      ?> 
     </div> 
    </body> 
</html> 

回答

0

如果您使用的MyISAM或InnoDB的引擎與MySQL,你可以使用一些內置的全文檢索功能。

你就可以做的東西,如:

"SELECT * FROM users WHERE MATCH (first_name, email) AGAINST ('john jones' IN BOOLEAN MODE)" 

這是使用PHP API和Ajax構建搜索作爲你型功能,例如真正有用的。

肯定有關於這些的閱讀,你可能需要做一些與你的數據庫工作,以確定它是否運行MyISAM或InnoDB,並可能有點配置。

http://dev.mysql.com/doc/refman/5.6/en/fulltext-search.html

http://dev.mysql.com/doc/refman/5.6/en/fulltext-boolean.html