欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript搜索自動提示功能的實現(xiàn)第2/3頁

 更新時間:2008年06月04日 23:08:08   作者:  
使用 jQuery(Ajax)/PHP/MySQL實現(xiàn)自動完成功我覺得我有必要寫這個教程,因為曾經(jīng)見到的大部分關(guān)于自動完成的應(yīng)用程序都只是給你一個程序源碼包,然后告訴你怎么使用,而不是告訴你它是如何工作的以及為什么這樣做。

PHP后臺程序(rpc.php):

如你所知(譯注:不好意思,看王小波就學會了這么個口頭禪),我的php后臺程序都叫做rpc.php(RPC指遠程過程調(diào)用),而沒用它實際執(zhí)行的功能來命名,但是也還不錯了。


復制代碼 代碼如下:

// PHP5 Implementation - uses MySQLi.  
$db = new mysqli(‘localhost', ‘root' ,”, ‘a(chǎn)utoComplete'); 
if(!$db) { 
    // Show error if we cannot connect. 
    echo ‘ERROR: Could not connect to the database.'; 
} else { 
    // Is there a posted query string? 
    if(isset($_POST[‘queryString'])) { 
        $queryString = $_POST[‘queryString']; 
        // Is the string length greater than 0? 
        if(strlen($queryString) >0) { 
        // Run the query: We use LIKE ‘$queryString%' 
        // The percentage sign is a wild-card, in my example of countries it works like this… 
        // $queryString = ‘Uni'; 
        // Returned data = ‘United States, United Kindom';  
        $query = $db->query("SELECT value FROM countries WHERE value LIKE ‘$queryString%' LIMIT 10"); 
        if($query) { 
            // While there are results loop through them - fetching an Object (i like PHP5 btw!). 
            while ($result = $query ->fetch_object()) { 
                // Format the results, im using <li> for the list, you can change it.            
                // The onClick function fills the textbox with the result. 
                echo ‘<li onclick="fill('‘.$result->value.'‘);">'.$result->value.‘</li>'; 
            } 
        } else { 
            echo ‘ERROR: There was a problem with the query.'; 
        } 
    } else { 
        // Dont do anything. 
    } // There is a queryString. 
} else { 
    echo ‘There should be no direct access to this script!'; 



?> 

PHP代碼解釋:

鑒于代碼中我已經(jīng)加了很多注釋,在這里我就不再說的很詳細了。

一般情況下,需要接收這個 ‘QueryString' 然后在其最后使用通配符產(chǎn)生一個查詢語句。

這意味著在這種情況下,每次敲進去一個字符都需要產(chǎn)生一個查詢語句,如果一直都這樣做的話,恐怕MYSQL會受不了。但是為了盡量的簡化這個過程,這種做法對一個規(guī)模較小的應(yīng)用應(yīng)該沒什么問題。

這段php代碼你需要在自己的系統(tǒng)中稍作修改,比如你需要更新‘$query'到你自己的數(shù)據(jù)庫,需要看在哪里放你數(shù)據(jù)庫表的列名等等。

CSS樣式:

我使用的是CSS3,天哪,它真的很好用,雖然在Firefox 或者Safari瀏覽器上會有功能限制。
復制代碼 代碼如下:

<style type="text/css">  
.suggestionsBox { 
    position: relative; 
    left: 30px; 
    margin: 10px 0px 0px 0px; 
    width: 200px; 
    background-color: #212427; 
    -moz-border-radius: 7px; 
    -webkit-border-radius: 7px; 
    border: 2px solid #000; 
    color: #fff; 


.suggestionList { 
    margin: 0px; 
    padding: 0px; 


.suggestionList li { 
    margin: 0px 0px 3px 0px; 
    padding: 3px; 
    cursor: pointer; 


.suggestionList li:hover { 
    background-color: #659CD8; 

</style> 

CSS代碼都很標準,沒什么需要特別指出的。

相關(guān)文章

最新評論