I am trying to implement the auto-complete script from http://www.devbridge.com/projects/autocomplete/jquery/. It's asking for JSON output like:
{
query:'Li',
suggestions:['Liberia','Libyan Arab Jamahiriya','Liechtenstein','Lithuania']
}
I am using PHP/MySQL. My query to get the suggestions would b开发者_JS百科e something like...
<?
$drug = $_GET['drug'];
$query = mysql_query("SELECT * FROM tags_drugs WHERE drug_name LIKE '$drug%'");
while ($query_row = mysql_fetch_array($query))
{
$drug_name = $query_row['drug_name'];
}
?>
This is where I'm stuck. How do I put the array $drug_name in the suggestions and encode it for json? Thanks in advance!
Use
$drug_name[] = $query_row['drug_name'];
instead of $drug_name = $query_row['drug_name'];
Then use
?>
<script type="text/javascript">
var drugName = <?php echo json_encode($drug_name);?>
</script>
Use this grugName
variable in your JavaScript.
json_encode($drug_name)
From PHP.net - json_encode
精彩评论