Jump to content

[SOLVED] Populating Option field from a php file


acctman

Recommended Posts

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
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.