简体   繁体   中英

Php Mysql Search using Jquery Autocomplete

I need to display search results using jQuery Autocomplete feature, I have to display results like this site when searching on brand name.

I have categories in table, when searching with brand name results should be display like below

Example: I am searching for Samsung. Results should be show like below

<pre>
Samsung
 in Mobiles
 in Tablets
</pre>

I have provided table image for understanding easy. If user type brand name that brand name should bring parent categories under list. In this image samsung name had two parent categories one is Mobiles and another is Tablets.

I am using this to get results but only category names displaying.

$term = $_GET["term"];
    $json=array();
    $st = $db->prepare("select * from category where name like '".$term."%' " );
    $st->execute();
    while($row = $st->fetch(PDO::FETCH_ASSOC))
    {
    $json[]=array(
                'value'=> $row["name"],
                'label'=>$row["name"]
                    );
    }
    echo json_encode($json); 

表格图片

You are assigning both value and label to == $row['name']. Label should be set to equal the parent category field of your table.

This:

$json[]=array(
            'value'=> $row["name"],
            'label'=>$row["name"]
                );
}

Should become:

$json[]=array(
            'value'=> $row["name"],
            'label'=>$row["url"]
                );
}

EDIT: changed wording + 'label' value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM