what is a good way

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ccrevling
Forum Newbie
Posts: 19
Joined: Mon Aug 06, 2007 1:34 pm

what is a good way

Post by ccrevling »

to make a dynamic drop down for when someone selects a country i need it to populate the states or counties list for each country that is choosen... and i have tried a dynamic drop down for javascript but i dont know how i would be able to get the java to query the database... Can someone help me please?

Heres my code for the drop downs:

Code: Select all

 
        <strong>Country:</strong>&nbsp;
            <select name="country" id="country" class="textbox">
             <?php 
             $country = $db->select("SELECT * FROM cc_iso_countries");
             for($i=0;$i<count($country); $i++){ ?>
             <option id="<?php echo $country[$i]['numcode']; ?>" value="<?php echo $country[$i]['id']; ?>"><?php echo $country[$i]['printable_name']; ?></option>
             <?php } ?>
            </select>
            <select name="state" id="state" class="textbox">
             <?php 
             $country = $db->select("SELECT * FROM cc_iso_counties WHERE");
             for($i=0;$i<count($country); $i++){ ?>
             <option id="<?php echo $country[$i]['numcode']; ?>" value="<?php echo $country[$i]['id']; ?>"><?php echo $country[$i]['printable_name']; ?></option>
             <?php } ?>
            </select>
 
And my database is set up like this for those 2 selects
--------------------------------------------------------------------
iso_countries iso_counties
id-------------------------+ id
iso +-----------country_id
printable_name abbrev
iso3 name
numcode

and the url for the javascript code i was trying to use is here
http://www.dynamicdrive.com/dynamicinde ... /index.htm
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: what is a good way

Post by kryles »

ok this may not be the best solution but something I have done in a similar circumstance.

In your php have an onchange event for the first dropdown

Code: Select all

<select name="country" onchange="popState"><option value="US">USA</option><option value="CAN">CANADA</option>
 
In your javascript use

Code: Select all

 
function popState()
{
  var country = document.forms["formName"].country.value;
  switch(country){ 
  case "CAN":
 <?php
     $query = "SELECT StateName, isoCode FROM table where countryCode = 'CAN'";
     $result = mysql_query($query);
     $state = "<select name=\"state\" id=\"state\">";
     while($row = mysql_fetch_array($result))
     {
        $state .= "<option value=\"$row[1]\">$row[0]</option>";
     }
            $state .= "</select>";
 document.getElementById("tableID").rows[Rowindex].cells[ColumnIndex].innerHTML = '<?php echo $state; ?>';
 
   } //end select
} //end function
 
Ok so basically when the page loads it'll fill in all the states for each case in your switch statement. When the onChange event is fired from your form it'll check what the iso Code is and run the query for that country. This only works if you have it set up as a table, but this will fill in the specified table cell with the value of $state (which is all the states in option tags). Does this help at all?

By the way, in my case there were only 3 countries, I don't know if performance would be an issue if you have a lot of countries when the page queries for all of the states originally
User avatar
hawkenterprises
Forum Commoner
Posts: 54
Joined: Thu Feb 28, 2008 9:56 pm
Location: gresham,oregon
Contact:

Re: what is a good way

Post by hawkenterprises »

Using a drop down menu that grabs from a database is a bad idea, at least in the way it intended to be implemented.

Countries rarely change.

What I recommend is using something like the following array and just load it client side

Code: Select all

 
 
       $continent["north america"] = array ("united states, canada");
       $continent["south america"] = array("brazil, columbia ....");
        ...
 
Does that make sense?
ccrevling
Forum Newbie
Posts: 19
Joined: Mon Aug 06, 2007 1:34 pm

Re: what is a good way

Post by ccrevling »

hawkenterprises wrote:Using a drop down menu that grabs from a database is a bad idea, at least in the way it intended to be implemented.

Countries rarely change.

What I recommend is using something like the following array and just load it client side

Code: Select all

 
 
       $continent["north america"] = array ("united states, canada");
       $continent["south america"] = array("brazil, columbia ....");
        ...
 
Does that make sense?
sorry no it doesnt...
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: what is a good way

Post by kryles »

Okay so you'll have arrays for each continent (storing the countries), and an array for each country (storing all the states/provinces)?
ccrevling
Forum Newbie
Posts: 19
Joined: Mon Aug 06, 2007 1:34 pm

Re: what is a good way

Post by ccrevling »

ok so how would i make that dynamic?

would i just do something like this (now this may not be that correct i know)

Code: Select all

 
$query = mysql_query("SELECT *,c.id AS c_id FROM iso_countries C LEFT JOIN iso_counties c ON c.countryId = C.id")
 
while ($row = mysql_fetch_array($query, MYSQL_ASSOC)){
 $continent[$row['id']] = array ($row['c_id']);
}
?>
 
does that look right?

then should i use a onselect to make it work or what from here?
Post Reply