[英]LIKE on three columns. Show login, firstname ,and lastname on autocomplete list but only select login when user selects
我有这个php:
$sql = ("SELECT login, firstname, lastname FROM $temp WHERE login LIKE '%$q%'");
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
$login = $rs['login'];
$fname = $rs['firstname'];
$lname = $rs['lastname'];
echo "$login ";
echo "$fname ";
echo "$lname \n";
}
我已经对变量进行了转义,等等。我想知道如何在三列上使用LIKE,用户如何在自动完成列表中看到所有三个值,然后再选择就只能在文本区域中看到登录名。 我正在使用jQuery自动完成插件顺便说一句:
<script type="text/javascript">
$().ready(function() {
$("textarea#user").autocomplete("autocomplete.php", {
width: 260,
matchContains: true,
selectFirst: false
});
});
在您的SQL中,您可能需要以下内容:
... where `login` like '...' or `firstname` like '...' or `lastname` like '...'
这样三个字段中的任何一个都将匹配。
jquery.autocomplete插件有一些变体。 我使用了其中之一,下面的代码对我很有效。
$("textarea#user").autocomplete("autocomplete.php", {
width: 260,
matchContains: true,
selectFirst: false,
formatItem: function(row, pos, num, query) {
// alert(row.join(',')); // See this alert, I don't remember in which order substrings are packed into "row"
// Return html we want to be displayed in an item within the dropdown list.
return row[0] + '<br /><em>' + row[1] + '</em> ' + row[2];
}
}).result(function(event, data, formatted) {
// alert(data.join(',')) // See what's in the "data", there should be your "login", "firstname", "lastname" of the item selected by user
// Do what you want here with the event.
$("textarea#user").val(data[0]);
});
我还没有测试代码。
同样,.result(...)部分对于您的情况似乎是不必要的,自动完成功能会将行的第一个子字符串插入输入中,我只是在说明您可以对“ item selected”事件进行自定义处理。
干杯。
编辑:
哦,我忘记了您可能还需要两个自动完成选项:matchContains:'word',matchSubset:false,
您无法在查询中使用LIKE
来自动完成。
您需要做这样的事情
$().ready(function() {
$("textarea#field1").autocomplete("autocompleteField1.php", {
width: 260,
matchContains: true,
selectFirst: false
});
});
$().ready(function() {
$("textarea#field2").autocomplete("autocompleteField2.php", {
width: 260,
matchContains: true,
selectFirst: false
});
});
在您的php文件中
$sql = ("SELECT DISTINCT firstname FROM $temp WHERE login LIKE '%$q%' LIMIT 0,10");//You don't want to have 20 autocomplete fields
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd, MYSQL_ASSOC)) {
echo "$rs['firstname']";
}
您使用的插件已弃用。 如果您还没有做很多工作,我建议您使用另一个具有更好支持且效果很好的插件。
对于其他jQuery插件 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.