开发者

AutoSuggest Using PHP/MySQL & Ajax

So I've created a autosuggest for my search engine similar to google using PHP/MYSQL and Ajax. If my MySQL has 2 different title of the same name how do i get only one of them to appear on autosuggest? For example: I have a field with title= ufc 131 and another with title=ufc 131 . When I search for UFC 131 how i get only one of them to show?

The code i use is..

<?php
    include('conn.php');
    $str = strtolower($_GET['content']);
    if(strlen($str))
    {
        $sel = mysql_query("select * from Streams where title like '".trim($str)."%'");
        if(mysql_num_rows($sel))
        {
            echo "<table border =\"0\" width=\"100%\">\n";
            if(mysql_num_rows($sel))
            {
                echo "<script language=\"javascript\">box('1');</script>";
                while($row = mysql_fetch_array($sel))
                {
                    $country = str_ireplace($str,"<b>".$str."</b>",($row['title']));
                    echo "<tr id=\"word".$row['title']."\" onmouseover=\"highlight(1,'".$row['title']."');\" onmouseout=\"highlight(0,'".$row['title']."');\" onClic开发者_开发百科k=\"display('".$row['title']."');\" >\n<td>".$country."</td>\n</tr>\n";
                }
            }
            echo "</table>";
        }
    }
    else
    {
        echo "<script language=\"javascript\">box('0');</script>";
    }
?>


Well, you can use the DISTINCT function, you can use the GROUP BY function, or you can even put all of your search results in an array in PHP after fetching it from MySQL and then using the array_unique() function to filter out duplicates.

But none of that is as efficient as making sure that the database doesn't contain any duplicates. What I mean to say is that, since you are providing a suggestion, you want the 'SELECT' element to run as fast as possible. Any type of trickery, like the ones I listed above, will require a combination of more time, more memory, and more CPU. Therefore, what you want to do is make sure that the database is in a good condition so that the SELECT statements can be as smooth as possible. A good way to do this is to make the 'title' field in your 'Streams' table a UNIQUE index. However, what I personally have, is a FULLTEXT index on my 'suggestion field'. The UNIQUE index will make sure there won't be any duplicates in the database for sure, but all that does it let you get away with sloppy PHP code for the insert. If you use proper FULLTEXT search, you can do FULLTEXT searches (MATCH() AGAINST()). Since those searches uses the index, they are performed MUCH faster. Your query statement will with no type of index actually use the index.

On top of that, you probably also want to update your PHP (/ echo) code some bit, cause, though your code is really small, it is not written optimally (which again, is something you WANT to make sure, in the case of search suggestions).


your code needs to be revamped. A good way for making autosuggestion is to use the minimum amount of data possible. So in this case you can use JSON.

include('conn.php');
$str = strtolower($_GET['content']);
$str = mysql_real_escape_string($str); // escape to prevent sql injection

Now for your query, You can use the GROUP BY keyword in mysql to not have duplicate lines

$sel = "SELECT title FROM Streams
        WHERE title LIKE '{$str}%'
        GROUP BY title 
        LIMIT 10";

Now for the returned data you can return it in Javascript object notation JSON

$data = "{";
while($row = mysql_fetch_array($sel)) {
   $data .= '"title":"'.$row['title'].'",';
}
$data .= "}";

now you can echo the content

echo $data;

In your javascript you can use a library like Jquery to make it easy to retrieve the lightweight json data

I hope this helps you make a very good autosuggestion box

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜