acctman Posted September 3, 2008 Share Posted September 3, 2008 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 <?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 ?> Link to comment https://forums.phpfreaks.com/topic/122595-solved-populating-option-field-from-a-php-file/ Share on other sites More sharing options...
Adam Posted September 3, 2008 Share Posted September 3, 2008 how are they stored in the countries file ? do you have say.. AL=Alabama|NY=New York|...etc ? Link to comment https://forums.phpfreaks.com/topic/122595-solved-populating-option-field-from-a-php-file/#findComment-633005 Share on other sites More sharing options...
Adam Posted September 3, 2008 Share Posted September 3, 2008 Actually why not store them in the database too? Use a 'relational database'.. Google it.. Link to comment https://forums.phpfreaks.com/topic/122595-solved-populating-option-field-from-a-php-file/#findComment-633009 Share on other sites More sharing options...
acctman Posted September 3, 2008 Author Share Posted September 3, 2008 Quote how are they stored in the countries file ? do you have say.. AL=Alabama|NY=New York|...etc ? the code i provide above is the countries_states.php file Link to comment https://forums.phpfreaks.com/topic/122595-solved-populating-option-field-from-a-php-file/#findComment-633018 Share on other sites More sharing options...
acctman Posted September 3, 2008 Author Share Posted September 3, 2008 any ideas? I already have all the states and countries loaded in a .php file. the coding above is a snippet from the php file to show how everything is structured Link to comment https://forums.phpfreaks.com/topic/122595-solved-populating-option-field-from-a-php-file/#findComment-633191 Share on other sites More sharing options...
Adam Posted September 3, 2008 Share Posted September 3, 2008 Ah yeah, to form an option of all the states.. foreach ($states['US'] as $state) print '<option value="' .$state. '">' .$states['US'][$state]. '</option>'; (Not checked) and to find the full title after selecting from database: $selectState = mysql_query("SELECT m_country, m_state FROM yourTable"); while ($stateRecord = mysql_fetch_assoc($selectState)) { if ($stateRecord['m_country'] == 'US') print 'US State: ' .$states['US'][ $stateRecord['m_state'] ]. '<br />'; } (Again not checked!) Adam Link to comment https://forums.phpfreaks.com/topic/122595-solved-populating-option-field-from-a-php-file/#findComment-633197 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.