开发者

Live search using AJAX: How to implement the server-portion

I'm building a simple case management systen for the agents on my company and I thought it would be nice to implement some form of search-suggestion-thingie, much like Google or (more recently) wikipedia has.

So, here´s the situation, and a question:

I have a table (or, rather, a view) with the following columns:

1. Firstname,
2. Lastname,
3. Phone,
4. Category
5. Owner,
6. Status,
7. Creator

(For our sanities sake lets assume every column is of type text or (n)varchar or 
any other representation of a 'string')

I could of course simply search each column and then present the result, but the script would have no way to know which result (or 'suggestion') would be the most relevant to the user.

So, how do you actually implement a live search from the servers point of view?

I would, of course, prefer to have the search done via SQL, but I can´t use stored procedures, so albeit probably possible, it´s r开发者_JAVA技巧ather limiting.

EDIT: (for clarification): I want to search my columns and return the result that is closest, and most relevant, to what the user searches (similar to what Google does). Preferably via SQL, but PHP is possible to use if it is reasonably fast.


First you need an url you can pass a request to. It is important that you load as little other resources there as possible. Since you're only going to perform a query and return a response it would be good to avoid bootstrapping a large part of your framework (if you're using one). This is important because you want the search hint request to be speedy.

So the url would be example.com/search/searchterm or example.com/search?searchterm=searchterm.

That url should route to a script where you read out the searchterm and perform a search for it. Whether you're using a MySQL database, a SOLR server or something else doesn't really matter (Code wise that is. MySQL is not optimized for full-text search but thats a whole other topic)

The result of searchterm could be returned as JSON Encoded string. This is easily handled in javascript (I suppose the client side implementation of your problem will be using javascript)

The MySQL way could be something like this:

search.php

$searchTerm = real_escape_string($_GET['searchterm']);

$sql = "SELECT `lastname` FROM `tablename` WHERE `lastname` LIKE '%" . $searchTerm . "%'";

//Use the result of the query to build a piece of HTML OR return a JSON encoded array and build the HTML client side.
$output = ....

header('Content-type: application/json');
echo PBJSON::encode($output);

EDIT:

After understanding the actual question from Hannes, I suggested to use the levenshtein to check which of the columns found was the best match. This function checks the difference between two strings and returns it. Using that you could see which of the columns was the closest match and use this information to decide what you will be returning.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜