我有一个脚本,JQuery UI自动完成功能完美。有一个搜索过程显示用户名和姓氏。但在我的数据库中,也有用户的照片,我想用fristname和lastname的建议显示它。在JQuery UI自动完成中显示图像
(在数据库中,PIC包含图像URL)
脚本:
$(function() {
$("#search").autocomplete({
source: "autocomplete.php",
minLength: 1,
select: function(event, ui) {
var url = ui.item.id;
if(url != '') {
location.href = '...' + url;
}
},
html: true,
open: function(event, ui) {
$(".ui-autocomplete").css("z-index", 1000); \t \t \t
}
}); \t
});
autocomplete.php
<?php
if (!isset($_REQUEST['term'])) {
\t exit;
}
$mysqli = new MySQLi($DB_host,$DB_login,$DB_pass,$DB_select);
$term = trim(strip_tags($_GET['term']));
$term = preg_replace('/\s+/', ' ', $term);
$a_json = array();
$a_json_row = array();
$a_json_invalid = array(array("id" => "#", "value" => $term, "label" => "Only letters and digits are permitted..."));
$json_invalid = json_encode($a_json_invalid);
if(preg_match("/[^\040\pL\pN_-]/u", $term)) {
print $json_invalid;
exit;
}
if ($data = $mysqli->query("SELECT * FROM users WHERE firstname LIKE '%$term%' OR lastname LIKE '%$term%' ORDER BY firstname , lastname")) {
\t while($row = mysqli_fetch_array($data)) {
\t \t $firstname = htmlentities(stripslashes($row['firstname']));
\t \t $lastname = htmlentities(stripslashes($row['lastname']));
\t \t $id= htmlentities(stripslashes($row['id']));
\t \t $pic= htmlentities(stripslashes($row['pic']));
\t \t $a_json_row["id"] = $id;
\t \t $a_json_row["value"] = $firstname.' '.$lastname;
\t \t $a_json_row["label"] = $firstname.' '.$lastname;
\t \t array_push($a_json, $a_json_row);
\t }
}
/* jQuery wants JSON data */
echo json_encode($a_json);
flush();
?>
请帮忙。
谢谢你的答案,但我想借此从数据库中的照片。每个用户都有不同的个人资料照片。所以我想在建议中显示个人资料图片,然后显示名字和姓氏。 – Dilak
我的例子从自动完成请求的json的'img'源信息....因此从你的数据库。 –
所以我应该这样做$ a_json_row [“img”] = $ pic;在autocomplete.php? – Dilak