ugnuku ugnuku - 1 year ago 66
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..



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

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

if(strlen($queryString) >0) {
$q = "SELECT keyword
SELECT tname AS keyword FROM t
SELECT sname AS keyword FROM sub
SELECT cname AS keyword FROM c
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) {
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {

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


can anybody help me to this?
Thank you.

Answer Source

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


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

foreach($results as $result)
        case 1:

        case 2:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download