0

我在搜索文本框中使用了自动完成框。它对我来说正常工作......这是我的脚本..

<?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>

有人可以帮我吗?谢谢你。

4

1 回答 1

1

使用搜索文本输入附近的下拉菜单选择类别或多个复选框以查看您必须创建的联合?我会推荐你​​狮身人面像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;
    }

}
于 2012-11-26T08:14:35.997 回答