Reputation: 181
Im currently using This JQuery as a search box on my website. The Javascript currently calls in values from a .txt file for the autocomplete results. However as I want my site to eventually have the option of the user adding these values, I'd like It to call in values from my database table instead.
JQuery on index page that calls the .txt file:
$(document).ready(function(){
$("#select3").fcbkcomplete({
json_url: "data.txt",
addontab: true,
maxitems: 10,
input_min_size: 0,
height: 10,
cache: true,
newel: true,
select_all_text: "select",
});
});
Formatting of the .txt file:
[{"key": "hello world", "value": "hello world"}, {"key": "movies", "value": "movies"},
I thought the solution would be instead of calling data.txt, to call data.php and have this code pull in values:
$getData = mysql_query("SELECT Name FROM tblCocktails");
while($info = mysql_fetch_array($getData))
{
echo "key: value:".$item['Name']"key: value:".$item['Name']';
}
however this doesn't seem to work and my debugging on dreamweaver has decided to not work. Any help would be appreciated.
Upvotes: 0
Views: 1280
Reputation: 55
i would use json_encode() instead a hardcoded string
//if you want other field of the database like value, I mofidied your query for this
$getData = mysql_query("SELECT Name, Value FROM tblCocktails");
$index=0;
while($info = mysql_fetch_array($getData))
{
//this will be storage every row of your table in an array until while is finish
$value[$index]=new Array("key"=>$info['Name'],
"value"=>$info['Value']
);
$index++;
}
//convert the array into a json string
echo json_encode($value);
this should output something like you need
Upvotes: 2
Reputation: 939
Your response must be on json format, try this in your php file:
$getData = mysql_query("SELECT Name FROM tblCocktails");
$data = array();
while($info = mysql_fetch_assoc($getData))
{
$data[] = $info['Name'];
}
echo json_encode($data);//data array is converted in json format response
Upvotes: 1
Reputation: 995
In while loop look at the echo
line. You should replace item with info.
Like this.
$getData = mysql_query("SELECT Name FROM tblCocktails");
while($info = mysql_fetch_array($getData))
{
echo "key: value:".$info['Name']."key: value:".$info['Name'];
}
Upvotes: 1