johntp Posted June 4, 2010 Share Posted June 4, 2010 Hello, We are currently using the following code to have state, and zip codes auto-filled when you choose a city. I would like to convert it to a database that it gets the info from. I have tried many different ways with no success. Could somone please give me some advice as to how i can do this? <?php $cities = array( array('city'=>'New York', state=>'NY', zip=>'10001'), array('city'=>'Los Angeles', state=>'CA', zip=>'90001'), array('city'=>'Chicago', state=>'IL', zip=>'60601'), array('city'=>'Houston', state=>'TX', zip=>'77001'), array('city'=>'Phoenix', state=>'AZ', zip=>'85001'), array('city'=>'Philadelphia', state=>'PA', zip=>'19019'), array('city'=>'San Antonio', state=>'TX', zip=>'78201'), array('city'=>'Dallas', state=>'TX', zip=>'75201'), array('city'=>'San Diego', state=>'CA', zip=>'92101'), array('city'=>'San Jose', state=>'CA', zip=>'95101'), array('city'=>'Detroit', state=>'MI', zip=>'48201'), array('city'=>'San Francisco', state=>'CA', zip=>'94101'), array('city'=>'Jacksonville', state=>'FL', zip=>'32099'), array('city'=>'Indianapolis', state=>'IN', zip=>'46201'), array('city'=>'Austin', state=>'TX', zip=>'73301'), array('city'=>'Columbus', state=>'OH', zip=>'43085'), array('city'=>'Fort Worth', state=>'TX', zip=>'76101'), array('city'=>'Charlotte', state=>'NC', zip=>'28201'), array('city'=>'Memphis', state=>'TN', zip=>'37501'), array('city'=>'Baltimore', state=>'MD', zip=>'21201'), ); // Cleaning up the term $term = trim(strip_tags($_GET['term'])); // Rudimentary search $matches = array(); foreach($cities as $city){ if(stripos($city['city'], $term) !== false){ // Add the necessary "value" and "label" fields and append to result set $city['value'] = $city['city']; $city['label'] = "{$city['city']}, {$city['state']} {$city['zip']}"; $matches[] = $city; } } // Truncate, encode and return the results $matches = array_slice($matches, 0, 5); print json_encode($matches); ?> Link to comment https://forums.phpfreaks.com/topic/203899-php-mysql-array/ Share on other sites More sharing options...
mrMarcus Posted June 4, 2010 Share Posted June 4, 2010 very simple. setup your db with a table named locations? then create three fields named `city`, `state`, `zip`. Assuming those are all the values (about 15-20), you can input them manually, no problem. Link to comment https://forums.phpfreaks.com/topic/203899-php-mysql-array/#findComment-1067934 Share on other sites More sharing options...
johntp Posted June 4, 2010 Author Share Posted June 4, 2010 I don't think I explained well enough, but thank you for your answer. I created the database "location" with the columns "city", "state", and "zip". I inputed the information in the database. My question is how do I output the MYSQL query into an array. I have tried the following but I think I'm doing it wrong. <?php //error_reporting(E_ALL); //ini_set('display_errors', '1'); include "./php/connect.php"; $query_users = "SELECT * FROM location"; $result = mysql_query($query_users); $new_array = array(); while ($row = mysql_fetch_assoc($result)) { $new_array["$row[0]"] => $row[1]; } return $new_array; ?> Link to comment https://forums.phpfreaks.com/topic/203899-php-mysql-array/#findComment-1067942 Share on other sites More sharing options...
ohdang888 Posted June 5, 2010 Share Posted June 5, 2010 you need to read up on mysql_fetch_array. with the while statement, that will turn $result into (usually) a one dimensional array. so $row[0] or $row[1] doesn't exist... rather its $row['city'] etc Link to comment https://forums.phpfreaks.com/topic/203899-php-mysql-array/#findComment-1068007 Share on other sites More sharing options...
ohdang888 Posted June 5, 2010 Share Posted June 5, 2010 and return $new_array; doesn't do anything... you needa use print_r($new_array); Link to comment https://forums.phpfreaks.com/topic/203899-php-mysql-array/#findComment-1068008 Share on other sites More sharing options...
johntp Posted June 5, 2010 Author Share Posted June 5, 2010 I appreciate all the help. I figued it out. <?php include "./php/connect.php"; // Data could be pulled from a DB or other source // Read records $result = mysql_query("SELECT * FROM location;") or die(mysql_error()); // Put them in array for($i = 0; $cities[$i] = mysql_fetch_assoc($result); $i++) ; // Delete last empty one array_pop($cities); // Cleaning up the term $term = trim(strip_tags($_GET['term'])); // Rudimentary search $matches = array(); foreach($cities as $city){ if(stripos($city['city'], $term) !== false){ // Add the necessary "value" and "label" fields and append to result set $city['value'] = $city['city']; $city['label'] = "{$city['city']}, {$city['state']} {$city['zip']}"; $matches[] = $city; } } // Truncate, encode and return the results $matches = array_slice($matches, 0, 5); print json_encode($matches); ?> Link to comment https://forums.phpfreaks.com/topic/203899-php-mysql-array/#findComment-1068263 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.