ugnuku ugnuku - 7 months ago 12
SQL Question

Categorize auto complete data

I used auto complete box to my searching text box. Its working properly for me... this is my script..

<?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!';
}

?>


problem is my autocomplete box display all result in subjects,tutor names, institutes etc to related keyword. Now I want to categorize them in autocomplete box.. ex: subjects under subject category, tutor name under tutor category etc... what i am trying to do is something similar to facebook search option..

This is my 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>


can anybody help me to this?
Thank you.

Answer

Use a drop down near the search text input to select category or multiple checkbox to see what union you would have to make? I would recommend you Sphinx http://sphinxsearch.com/

Try

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;
    }

}