2

Do you have an idea how to pull data from mysql, put it in an array then feed it in an autocomplete field?

I have tried hardcoded the values but what I'm thinking is when I add a new record, I have to re-code again the array. I'm a newbie in PHP so I beg your pardon.

Kindly check what I've tried so far:

protected function jsGenerateResourcesAutocomplete(){


    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';

    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
        die('Could not connect: ' . mysql_error());
    }

    $sql = "SELECT employee_name FROM employee" ;

    mysql_select_db('test', $conn);
    $retval = mysql_query($sql);

    $row = mysql_fetch_assoc($retval); 


    if(!$retval )
    {
        die('Could not select data: ' . mysql_error());
    } 

    $employeeNames = $this->employeeNames;
    $html = "";     $html .= 'var employeenames = [' . PHP_EOL;
    foreach ($employeeNames as $employeeName) {
        $html .= '"' . $employeeName-> $array . '",' . PHP_EOL;
    }
    $html .= '];';
    $html .= '$(".resource-input input").autocomplete({source: employeenames});' .PHP_EOL;
    $html .= '});' . PHP_EOL; 
    $html .= '</script>' . PHP_EOL;
    return $html;
} 

I believe something in this line:

$html .= '"' . $employeeName-> $array . '",' . PHP_EOL;

I have to put forth the array but I have no idea how to do it. Any help is truly appreciated. Thanks.

5
  • what html does this output? Commented Dec 18, 2012 at 8:39
  • hard to understand your code.but if you want to make javascript array from php array. then you can use json_encode($array); Commented Dec 18, 2012 at 8:40
  • I still don't have an output. But using the hardcoded, it does the autocomplete. Commented Dec 18, 2012 at 8:41
  • 1
    @GBD I'd like to get the data from mysql, do you have an idea to do that? Commented Dec 18, 2012 at 8:46
  • 1
    Heads up! The next major release of PHP is deprecating the mysql_ family of functions. Now would be a great time to switch to PDO or mysqli. Commented Dec 18, 2012 at 9:27

1 Answer 1

1

Try this way:

$sql = "SELECT employee_name FROM employee" ;

mysql_select_db('test', $conn);
$retval = mysql_query($sql);

if(!$retval )
{
    die('Could not select data: ' . mysql_error());
} 
$data = array();

while($row = mysql_fetch_array($retval)){
  $data[] = $row['employee_name'];
}

$html = "<script>";
$html .= 'var employeenames = '.json_encode($data);
$html .= '$(".resource-input input").autocomplete({source: employeenames});';
$html .= '});'; 
$html .= '</script>';
return $html;
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry, it's now fixed, but the json_encode($data) doesn't show the autocomplete?
is this SELECT employee_name FROM employee query running directly in your database

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.