0

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 be 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!

2 Answers 2

2

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.

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

Comments

0
json_encode($drug_name)

From PHP.net - json_encode

Comments

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.