Convert static array to dynamic (from MySql table)

Hi ALL,

I’m using an ajax script for autocomplete an input text. The ORIGINAL DEMO script uses a static array to echo some user names at the input text :


$names = array('Abraham Lincoln', 'Adolf Hitler', 'Agent Smith', 'Agnus', 'AIAI', 'Akira Shoji', 'Akuma', 'Alex', 'Antoinetta Marie', 'Baal', 'Baby Luigi', 'Backpack', 'Baralai', 'Bardock', 'Baron Mordo', 'Barthello', 'Blanka', 'Bloody Brad', 'Cagnazo', 'Calonord', 'Calypso', 'Cao Cao', 'Captain America', 'Chang', 'Cheato', 'Cheshire Cat', 'Daegon', 'Dampe', 'Daniel Carrington', 'Daniel Lang', 'Dan Severn', 'Darkman', 'Darth Vader', 'Dingodile', 'Dmitri Petrovic', 'Ebonroc', 'Ecco the Dolphin', 'Echidna', 'Edea Kramer', 'Edward van Helgen', 'Elena', 'Eulogy Jones', 'Excella Gionne', 'Ezekial Freeman', 'Fakeman', 'Fasha', 'Fawful', 'Fergie', 'Firebrand', 'Fresh Prince', 'Frylock', 'Fyrus', 'Lamarr', 'Lazarus', 'Lebron James', 'Lee Hong', 'Lemmy Koopa', 'Leon Belmont', 'Lewton', 'Lex Luthor', 'Lighter', 'Lulu');

I’d like to use the script to echo the user names from my MySql table ‘users’ and I’m trying to generate the ‘same’ array structure in order to leave the rest of the script intact. Can someone please give me an instruction? Thanks advanced!

Well, I’m not sure if that’s what I want to do…
The script is doing this :
TexBoxList Demo

The part of the script that is ‘in charge’ for adding the usernames is :


$response = array();

$names = array('Abraham Lincoln', 'Adolf Hitler', 'Agent Smith', 'Agnus', 'AIAI', 'Akira Shoji', 'Akuma', 'Alex', 'Antoinetta Marie', 'Baal', 'Baby Luigi', 'Backpack', 'Baralai', 'Bardock', 'Baron Mordo', 'Barthello', 'Blanka', 'Bloody Brad', 'Cagnazo', 'Calonord', 'Calypso', 'Cao Cao', 'Captain America', 'Chang', 'Cheato', 'Cheshire Cat', 'Daegon', 'Dampe', 'Daniel Carrington', 'Daniel Lang', 'Dan Severn', 'Darkman', 'Darth Vader', 'Dingodile', 'Dmitri Petrovic', 'Ebonroc', 'Ecco the Dolphin', 'Echidna', 'Edea Kramer', 'Edward van Helgen', 'Elena', 'Eulogy Jones', 'Excella Gionne', 'Ezekial Freeman', 'Fakeman', 'Fasha', 'Fawful', 'Fergie', 'Firebrand', 'Fresh Prince', 'Frylock', 'Fyrus', 'Lamarr', 'Lazarus', 'Lebron James', 'Lee Hong', 'Lemmy Koopa', 'Leon Belmont', 'Lewton', 'Lex Luthor', 'Lighter', 'Lulu');

// make sure they're sorted alphabetically, for binary search tests
sort($names);

foreach ($names as $i => $name)
{
	$filename = str_replace(' ', '', strtolower($name));
	$response[] = array($i, $name, null, '<img src="images/'. $filename . (file_exists('images/' . $filename . '.jpg') ? '.jpg' : '.png') .'" /> ' . $name);
}

header('Content-type: application/json');
echo json_encode($response);

Now I want the $names array to be the array with usernames from the MySql table ‘users’. I’ve built the query, I just can’t figure out how to define the array…

I think the array in the demo simply replaced the database you would normally use.

Since you have a database, then you don’t really need the array but instead you could could query the database directly instead of querying the array.

Also, if your database has 100’s or 1000’s of names, you wouldn’t normally extract them all into a large array.

Another option is along these lines:

 
<?php
 
$name = $_POST['txtName'];   //txtName is the partially entered name in the html form
 
$query = 'select fldName from tblUsers where fldName like "'.mysql_real_escape_string($name,$conn).'&#37;"';
$rs = mysql_query($query,$conn);
 
//now put the names in a select list
echo '<select id="selNames">';
while($row=mysql_fetch_assoc($rs)) {
         echo '<option value="'.$row['fldName'].'">'.$row['fldName'].'</option>';
}
echo '</select>';
 
?>

You would also need to include an onchange event handler in the <select> to input the selected name into the name textbox.

The ajax responseText containing the <select> can then be used to add the <select> to your web page.