4

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

Table Image

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

There is only one field in table that is name for categories. I am saying samsung is brand name but that is also category name.
I am printing category name successfully when user search, but under list parent category names logic I Need.
Please read my question again and, check site link from question.
In your image of your database there is a column that has "Samsung Mobile Phones", "Samsung-Tablets", "Apple-tablets" etc. Is that information coming from a different table? If not, what is the field name for that column?
filed name is url for that. I have running query where name like only so it is not important I think.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.