Page 1 of 1

Populating Option field from a php file

Posted: Wed Sep 03, 2008 5:01 pm
by acctman
Hi I have a countries and states php file that holds the abbrev. and names for states and countries. can someone assist me with creating an html <option> field to display the Country names in and as a value the countries abbrev. and the same would apply to the States.
Countries:
<OPTION value="US">United States</OPTION>

States:
<OPTION value="AL">Alabama</OPTION>

Also another question to save from having to post twice. once the values are saved into my database m_state and m_country how can I grab the full state/country name after i do a query? So i a user has m_state = AL and m_country = US how can i access the countries_states.php and echo/print Alabama and United States

thanks in advance

countries_states.php

Code: Select all

 
<?php 
$countries['AD'] = array(name => 'Andorra', lat => 42, lon => 1); 
$countries['AE'] = array(name => 'United Arab Emirates', lat => 24, lon => 54); 
$countries['AF'] = array(name => 'Afghanistan', lat => 34, lon => 69); 
// ... more shorten 
 
//sort countries alphabetically 
function sortby($a, $b) { 
if ($a[name] == 'United States') return -1; 
if ($a[name] == $b[name]) return 0; 
return ($a[name] > $b[name]) ? -1 : 1; 
} 
 
uasort($countries,"sortby"); 
$countries['US'] = array(name => 'United States', lat => 39, lon => -93, z => 3); 
$countries = array_reverse($countries, true); 
 
$states['US']['AL'] = 'Alabama'; 
$states['US']['AK'] = 'Alaska'; 
$states['US']['AZ'] = 'Arizona'; 
 
// ... more shorten 
?> 
 

Re: Populating Option field from a php file

Posted: Wed Sep 03, 2008 5:42 pm
by WebbieDave
Read about foreach's second syntax:
http://us.php.net/manual/en/control-str ... oreach.php

Code: Select all

foreach ($countries as $key=>$val) {
    echo "<option value=\"$key\">{$val['name']}</option>\n";
}

Re: Populating Option field from a php file

Posted: Thu Sep 04, 2008 8:06 am
by acctman
thanks