1

Here is the part of my Javascript:

$(function(){

var dbTags=<?php echo json_encode($namesArray);?>;
var myTag;

$("#searchTags").autocomplete({

  source: dbTags,
  minLength: 2,
  select: function(event, ui){

myTag=ui.item.value;

My php code:

$findNames=$user_home->runQuery("SELECT productTitle, category from Iranga ");
$findNames->execute();
$information=$findNames->fetchAll(PDO::FETCH_ASSOC);

foreach($information as $item){

   $namesArray=array("label:"=>$item['productTitle'], "category:"=>$item['category']);

}

Am planning to use this script: https://jqueryui.com/autocomplete/#categories

The question how to use and define my dbTags variable, when it contains product tilte and categories?

1 Answer 1

1

If you're following the example you linked you want your "source" array to be an array of objects with "label" and "category" properties. I'm not sure what your PHP output is, or what database api you're using (assuming PDO), but I would try:

$findNames=$user_home->runQuery("SELECT productTitle, category from Products");
$findNames->execute();
while ($row=$findNames->fetch()){
 $namesArray[] = array("label" => $row['productTitle'], "category" => $row['category']);
}
Sign up to request clarification or add additional context in comments.

5 Comments

"or what database api you're using" - OP is using PDO.
what is runQuery()?
I'm not the OP btw ;-) But that must be some custom function.
Yes it is my custom function which is preparing sql query.
That code will work if your custom database is using PDO. If it's not then you have to tell us what API it uses or figure out how to adjust my code yourself. The error implies that the $row array(?) doesn't contain 'productTitle'.

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.