Welcome Guest, Not a member yet? Register   Sign In
Best practice for creating long dropdowns. ie. for countries and US States
#1

[eluser]rhussain[/eluser]
Hi,

I'm a newbie with CI and I'm sure glad I discovered it!!! Thanks to all that have contributed to it.

My question is...

What would be the best practice for setting the large arrays that would be used to populate a dropdown for Countries and US States etc. Where should I be declaring the the arrays? I have them in the view at the moment but it makes a short file very long! Is there somewhere more approriate to declare the arrays?

By the way I'm using form_dropdown('country', $countries) in my view file.

Thanks in advance.
#2

[eluser]Atharva[/eluser]
You should use database to store countries and state. Here is how you can access countries

Code:
//in your model some_model.php

function countries()
{
  return $this->db->get('countries'); //select from countries table from
}
//in your controller

function some_function()
{
  $this->load->model('some_model');
  $data['countries'] = $this->some_model->countries();
  $this->load->view('some_view',$data);

}

//in your view some_view
<select name="cname">
<option value="0">Select Country</option>
&lt;?
if($countries->num_rows() > 0)
{
   foreach($countries->result() as $row)
  {
    ?&gt;
       <option value="&lt;?=$row->country_id?&gt;">&lt;?=$row->country_name?&gt;</option>
    &lt;?
  }
}
?&gt;
</select>
You can use form_dropdown function too.
Same goes for state.
#3

[eluser]LuckyFella73[/eluser]
Like Atharve I would do it with db tables too. There are several
sql files available to download with all the country data you need.

For example this one:
http://goneale.com/files/code/Iso3166CountryList.sql

Of course there are other sql file around the net - maybe this one allready fits to your needs.
#4

[eluser]rhussain[/eluser]
Thank you both for your replies. I'll take your advice.
@Atharva: And thanks for the code snippet.

I also found his link for the states:
http://snipplr.com/view/6637/mysql-table--us-states/




Theme © iAndrew 2016 - Forum software by © MyBB