Bassistance Autocomplete Plugin - Search Page Replacement
Posted
by Dante
on Stack Overflow
See other posts from Stack Overflow
or by Dante
Published on 2010-06-10T10:16:34Z
Indexed on
2010/06/10
11:52 UTC
Read the original article
Hit count: 380
Hi,
i've setup an autocomplete field that searches my database which works fine:
$("#autocomplete input#lookupAccount").autocomplete("lib/php/autocomplete_backend.php", {
width: 300,
selectFirst: false,
delay: 250 });
When a user clicks on a result I want to refer them to another page depending on what they've been clicking. In the documentation I find the following:
Search Page Replacement
An autocomplete plugin can be used to search for a term and redirect to a page associated with a resulting item. The following is one way to achieve the redirect:
var data = [ {text:'Link A', url:'/page1'}, {text:'Link B', url: '/page2'} ];
$("...").autocomplete(data, {
formatItem: function(item) {
return item.text;
}
}).result(function(event, item) {
location.href = item.url;
});
So i need to return the following from my PHP file : {text:'link A', url:'/page1'},...
But my PHP file now returns
$returnData = "<ul>";
if(isset($results)){
for($j=0; $j < count($results); $j++){
if($results[$j][0] == "account"){
if($j % 2){
$returnData .= "<li>".$results[$j][1]."<br /><i>".$results[$j][2].", ".$results[$j][3]." ".$results[$j][4]."</i></li>";
} else {
$returnData .= "<li style=\"background: blue\">".$results[$j][1]."<br /><i>".$results[$j][2].", ".$results[$j][3]." ".$results[$j][4]."</i></li>";
}
} else {
$returnData .= "<li style=\"background: yellow\"><i>".$results[$j][1]."</i> (".$results[$j][2].")</li>";
}
}
$returnData .= "</ul>";
echo $returnData;
} else {
echo "Sorry geen resultaten!";
}
So it loops through an array and returns an li value depending on what it finds in the array. How can I match that with: {text:'link A', url:'/page1'}???
© Stack Overflow or respective owner