开发者

return mysql rows in search as you type in php using jquery and caching

I'm trying to work o开发者_开发知识库n a search script that pulls up information about what you're searching and provides teh top hits the same way facebook does when you search for a name. I've looked into jquery's autocomplete but I don't think it's what i need. What is the most efficient way to do this? Is there a way to cache? I know how to do this, as in narrow down the search every time the user types, but that requires me to rifle back through the intial returned result and I don't know how to query a cached mysql result.

Any clues would be amazing


The only way to cache a mysql result is either to put it in a temporary table, or to store it in jQuery. The easiest way to store the result (assuming you have converted it into an array) is to use JSON. In your PHP script, just do the following:

$result = $db->query('select * from table');//Assume the query function converts the result into an array
echo json_encode($result);

What this does json_encode($result); is convert your array into JSON which can be sent back the browser where it is easily (and automatically) converted back into an array. Now, in jQuery you can use:

var cachedVar;
$.ajax({
   url: 'page.php', 
   dataType: 'json', 
   data: {'data':'here'}, 
   type: 'POST', 
   success: function(data) { cachedVar = data; }
});

The cachedVar = data; portion of that code will store the array into cachedVar. You can now access the array as you normally would anywhere else in your script and the results are "cached."


Create a table where all the search results are stored, upon searching your site, the searched items will be searched in that table also and the one with most search count will go on top. Hope this makes sense.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜