ragrim Posted October 2, 2011 Share Posted October 2, 2011 Hi, Im building a form that has an ajax autocomplete and im trying to get my info from mysql to load into the array, i have it working but it displays the first name and last name for each person as a seperate array item, for example i have "ben dover" it treats ben as 1 array item and dover as second array item. what i need is to add each person from clients field in my DB to display as LastName, FirstName and Client_ID needs to come accross into my hidden field. heres the php code $result = mysql_query("SELECT * FROM clients"); $aUsers = mysql_fetch_array($result); How do i add the 3 fields im pulling from DB into that array? Quote Link to comment https://forums.phpfreaks.com/topic/248253-php-array-help/ Share on other sites More sharing options...
mikesta707 Posted October 2, 2011 Share Posted October 2, 2011 Hmm i'm not quite sure what you are asking for. Could you perhaps clarify? Just guessing, but did you want to combine the first and last name into 1 string (to feed into your auto complete form or whatever)? If so, you simply need to concatenate the two values. I don't know your column names, but assuming LastName and FirstName are used, $first = $aUsers['FirstName']; $last = $aUsers['LastName']; $combined = "$last, $first"; Quote Link to comment https://forums.phpfreaks.com/topic/248253-php-array-help/#findComment-1274858 Share on other sites More sharing options...
ragrim Posted October 2, 2011 Author Share Posted October 2, 2011 sorry for the poor explanation im not sure how to explain it, this is the first time ive tried to build something like this. Im using this http://www.brandspankingnew.net/specials/ajax_autosuggest/ajax_autosuggest_autocomplete.html Now it supplies me a test.php that has a hard coded array, im trying to replace that array with data from my mysql database. When someone starts tying i want the text box to display the FirstName and LastName of clients, and when they select one it inputs the Client_ID of that person into the hidden field. Using the code above it pulls the data like this $aUsers = array( "ben", "dover" ) it automatically adds an id for each item in array what i would like to do is have it come accross as $aUsers = mysql_fetch_array ( "1","ben","dover" "2","next","user" ) and to use my primary id field which is client_id Quote Link to comment https://forums.phpfreaks.com/topic/248253-php-array-help/#findComment-1274861 Share on other sites More sharing options...
mikesta707 Posted October 2, 2011 Share Posted October 2, 2011 Ahh I see. Well in your PHP script are you sending back a JSON object, or XML? JSON would be easiest, as you can use PHP's built in json_encode function to have it convert the PHP array into json for you. To get the structure you want, based on what I've read, you will need to have a multidimensional array, or rather an array of associative arrays. You will need to loop through your result set in order to fill the array with all the information from your database. Note, as your database grows, this method will become more and more inefficient, but it doesn't sound like you have the type of numbers in your database that would make a difference. $array = array(); $while($row = mysql_fetch_array($result)){ $name = "$row[LastName], $row[FirstName]"; $id = $row['id']; $array[] = array("id" => $id, "value" =>$name, "info" => ""); } //the json objects needs to be of the strucuture: //*** { results : [ { id: "1", value: "value", info: "some info. can be blank"}, { .... }, { .... } ] ****/ //so we stick the whole thing into another array $jsonArray = array("results"=> $array); //now we can send our json array back in json echo json_encode($jsonArray); hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/248253-php-array-help/#findComment-1274863 Share on other sites More sharing options...
ragrim Posted October 2, 2011 Author Share Posted October 2, 2011 That helps alot in understanding how to build arrays, but i have just realised this is alot more complicated than i first thought, at the bottom of the php page was more code which i now see is the parts that builds the results, since my php skills are not that good im not sure how to use the code you provided. Heres the full code of the php <?php include('include/dbconnect.php'); $result = mysql_query("SELECT * FROM clients"); $aUsers = mysql_fetch_array($result); $input = strtolower( $_GET['input'] ); $len = strlen($input); $aResults = array(); if ($len) { for ($i=0;$i<count($aUsers);$i++) { // had to use utf_decode, here // not necessary if the results are coming from mysql // if (strtolower(substr(utf8_decode($aUsers[$i]),0,$len)) == $input) $aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) ); //if (stripos(utf8_decode($aUsers[$i]), $input) !== false) // $aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) ); } } header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header ("Pragma: no-cache"); // HTTP/1.0 if (isset($_REQUEST['json'])) { header("Content-Type: application/json"); echo "{\"results\": ["; $arr = array(); for ($i=0;$i<count($aResults);$i++) { $arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"\"}"; } echo implode(", ", $arr); echo "]}"; } else { header("Content-Type: text/xml"); echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>"; for ($i=0;$i<count($aResults);$i++) { echo "<rs id=\"".$aResults[$i]['id']."\" info=\"".$aResults[$i]['info']."\">".$aResults[$i]['value']."</rs>"; } echo "</results>"; } ?> I think i might be better off building my own ajax autocomplete as this code is a bit too complicated for me. Quote Link to comment https://forums.phpfreaks.com/topic/248253-php-array-help/#findComment-1274864 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.