分类自动完成数据

时间:2012-11-26 08:02:08

标签: php jquery mysql

我在搜索文本框中使用了自动完成框。它适合我......这是我的剧本..

<?php

require_once('database.php');

if(isset($_POST['queryString'])) {

    $queryString = $dbc->real_escape_string($_POST['queryString']);

    if(strlen($queryString) >0) {
        $q = "SELECT keyword 
                FROM (
                    SELECT tname AS keyword FROM t
                    UNION
                    SELECT sname AS keyword FROM sub
                    UNION
                    SELECT cname AS keyword FROM c
                    UNION
                    SELECT iname AS keyword FROM i
                ) s
              WHERE keyword LIKE '%$queryString%' 
              LIMIT 10";

        $r = mysqli_query ( $dbc, $q);

        if($q) {
            while ($row = mysqli_fetch_array($r, MYSQL_ASSOC)) {
                echo '<li onClick="fill(\''.$row['keyword'].'\');">'.$row['keyword'].'</li>';
            }
        } else {
            echo 'ERROR: There was a problem with the query.';
        }
    } else {

    }
} else {
    echo 'There should be no direct access to this script!';
}

?>

问题是我的自动填充框显示所有结果的主题,导师姓名,机构等相关关键字。现在我想在自动完成框中对它们进行分类..例如:主题类别下的主题,导师类别下的导师名称等...我想要做的是类似于facebook搜索选项...

这是我的jQuery:

<script type="text/javascript">

    function lookup(inputString) {

        if(inputString.length == 0) {
            $('#suggestions').hide();
        } else {
            $.post("rpc.php", {queryString: ""+inputString+""}, function(data){
                if(data.length >0) {
                    $('#suggestions').show();
                    $('#autoSuggestionsList').html(data);
                }
            });
        }
    }

    function fill(thisValue) {
        $('#inputString').val(thisValue);
        setTimeout("$('#suggestions').hide();", 200);
    }

</script>

任何人都可以帮助我吗? 谢谢。

1 个答案:

答案 0 :(得分:1)

使用搜索文本输入附近的下拉菜单选择类别或多个复选框,以查看您必须制作哪个联盟?我会向你推荐Sphinx http://sphinxsearch.com/

尝试

SELECT 1 as type,tname AS keyword FROM t
                    UNION
                    SELECT 2 as type,sname AS keyword FROM sub
                    UNION
                    SELECT 3 as type,cname AS keyword FROM c
                    UNION
                    SELECT 4 as type,iname AS keyword FROM i

foreach($results as $result)
{
    switch($result['type'])
    {
        case 1:
        $cat_1[]=$result;
        break;

        case 2:
        $cat_2[]=$result;
        break;
    }

}