2013-11-26 19 views
0

我想使用jqueryui的自動完成多個遠程小部件。我從網站上下載了它。它有一個search.php頁面來遠程檢索數據。我問的是,默認情況下,它使用某些已經給定值的PHP的array(),但我想用它從數據庫中檢索數據,因爲用戶鍵入查詢。search.php從jqueryui自動完成多個遠程小部件的數據庫

search.php中看起來是這樣的:

<?php 

sleep(3); 
// no term passed - just exit early with no response 
if (empty($_GET['term'])) exit ; 
$q = strtolower($_GET["term"]); 
// remove slashes if they were magically added 

if (get_magic_quotes_gpc()) $q = stripslashes($q); 
$items = array(
"Great Bittern"=>"Botaurus stellaris", 
"Little Grebe"=>"Tachybaptus ruficollis", 
"Black-necked Grebe"=>"Podiceps nigricollis", 
"Little Bittern"=>"Ixobrychus minutus", 
"Heuglin's Gull"=>"Larus heuglini" 
); 
$result = array(); 
foreach ($items as $key=>$value) { 
    if (strpos(strtolower($key), $q) !== false) { 
     array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key))); 
    } 
    if (count($result) > 11) 
     break; 
} 
// json_encode is available in PHP 5.2 and above, or you can install a PECL module in earlier versions 
echo json_encode($result); 
?> 

,而不是這個,我要的$項目的事情是從數據庫的(比如MY_DB)表(說我的表名爲用戶),我想從此表中提取名稱字段。

任何幫助將不勝感激。

回答

0

我對此很新穎,只是對自己有所瞭解,所以如果我錯了,請不要讓我失望,但是:

- >你不只是想運行一個mysql查詢,而不是在腳本內部建立一個數組?

因此,像:

$items = mysqli_query($database_connection, "SELECT * FROM my_db WHERE column_name LIKE '%$q%'"); 

就像我說我很新的這所以這可能是完全錯誤的,但我只是想幫助!

祝你好運!