Hi All,

I've hit a brick wall trying to get the jquery autocomplete to work correctly. The problem is with the .php file and the way the While loop works, as what it does is create a array for every name rather than one single array. I've tried to move the 'echo json_encode' outside of the loop but then it only returns the very last value.

So question is how do I make the While function build an array of every distinct name in the database then echo as json_encode.

Thanks

CODE:

<?php
$conn = new COM("ADODB.Connection") or die("Cannot start ADO"); 

$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\\Admin.mdb");

$queryString = $_GET['term'];

$query = "SELECT DISTINCT [PersonNameFirst] FROM Person WHERE PersonNameFirst like '$queryString%'";

$result = $conn->Execute($query);

while (!$result->EOF) 
    {
        $fz = $result->Fields("PersonNameFirst");
        $array = array($fz->value);
        $result->MoveNext();
        $output = $array;
        echo json_encode($output);  
    }

?>

Recommended Answers

All 4 Replies

at line 15 you are creating a new array for each and every record thats giving the invalid output to you

move that declarion out side of the while loop and make changes as follows

$my_array = array();
$rec_count =0;
while (!$result->EOF) 
    {
        $fz = $result->Fields("PersonNameFirst");
        $my_array[$rec_count] = $fz->value ;
        $result->MoveNext();        
        $rec_count++;
    }
echo json_encode($my_array);  

please check it once
let me know the status

Hi radhakrishna,

That worked perfectly I am now seeing a single array of results. Just one more question

On the line "$my_array[$rec_count] = $fv->value ' ' $fz->value" how do I get it join first name and lastname together with a space in between e.g. "John Smith"? json complain about my formatting.

Thanks again.

$query = "SELECT DISTINCT [PersonNameLast], [PersonNameFirst] FROM Person WHERE PersonNameFirst like '$queryString%'";

$result = $conn->Execute($query);

$my_array = array();
$rec_count = 0;

while (!$result->EOF) 
    {
        $fv = $result->Fields("PersonNameFirst");
        $fz = $result->Fields("PersonNameLast");
        $my_array[$rec_count] = $fv->value ' ' $fz->value;
        $result->MoveNext();
        $rec_count++;

    }
echo json_encode($my_array);    

replace this $my_array[$rec_count] = $fv->value ' ' $fz->value;
with the following

 $my_array[$rec_count] = $fv->value.' '.$fz->value;

in php '.' is used for concatenating operations

please check it
and let me know the status

happy coding

It worked. Thanks again for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.