Advertisement
Guest User

Untitled

a guest
Mar 19th, 2013
1,476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class User extends CI_Controller {
  3.  
  4.     public function __construct() {
  5.         parent::__construct();
  6.         $this -> load -> model('country_model');
  7.  
  8.     }
  9.  
  10.    
  11.     function Register() {
  12.         $data['countries'] = $this -> country_model -> get_countries();
  13.         $this -> load -> view('test/post_view', $data);
  14.     }
  15.    
  16.     function get_cities($country){
  17.       $this->load->model('city_model');
  18.       header('Content-Type: application/x-json; charset=utf-8');
  19.       echo(json_encode($this->city_model->get_cities($country)));
  20. }
  21.  
  22. }
  23. ?>
  24. <?php
  25. class City_model extends  CI_Model {
  26.  
  27.     public function __construct() {
  28.         $this -> load -> database();
  29.  
  30.     }
  31.  
  32.     function get_cities($country = null){
  33.       $this->db->select('id, city_name');
  34.  
  35.       if($country != NULL){
  36.           $this->db->where('country_id', $country);
  37.       }
  38.  
  39.       $query = $this->db->get('cities');
  40.  
  41.       $cities = array();
  42.  
  43.       if($query->result()){
  44.           foreach ($query->result() as $city) {
  45.               $cities[$city->id] = $city->city_name;
  46.           }
  47.       return $cities;
  48.       }else{
  49.           return FALSE;
  50.       }
  51. }
  52.  
  53. }
  54. ?>
  55. <?php
  56. class Country_model extends  CI_Model {
  57.  
  58.     public function __construct() {
  59.         $this -> load -> database();
  60.  
  61.     }
  62.  
  63.     function get_countries() {
  64.         $this -> db -> select('id, country_name');
  65.         $query = $this -> db -> get('countries');
  66.  
  67.         $countries = array();
  68.  
  69.         if ($query -> result()) {
  70.             foreach ($query->result() as $country) {
  71.                 $countries[$country -> id] = $country -> country_name;
  72.             }
  73.             return $countries;
  74.         } else {
  75.             return FALSE;
  76.         }
  77.     }
  78.  
  79. }
  80. ?>
  81.  
  82.  
  83. <html>
  84.    
  85.     <head>
  86.         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  87.         <script type="text/javascript">// <![CDATA[
  88.     $(document).ready(function(){      
  89.         $('#country').change(function(){ //any select change on the dropdown with id country trigger this code        
  90.             $("#cities > option").remove(); //first of all clear select items
  91.             var country_id = $('#country').val();  // here we are taking country id of the selected one.
  92.             $.ajax({
  93.                 type: "POST",
  94.                 url: "http://localhost/test/user/get_cities/"+country_id, //here we are calling our user controller and get_cities method with the country_id
  95.                  
  96.                 success: function(cities) //we're calling the response json array 'cities'
  97.                 {
  98.                     $.each(cities,function(id,city) //here we're doing a foeach loop round each city with id as the key and city as the value
  99.                     {
  100.                         var opt = $('<option />'); // here we're creating a new select option with for each city
  101.                         opt.val(id);
  102.                         opt.text(city);
  103.                         $('#cities').append(opt); //here we will append these new select options to a dropdown with the id 'cities'
  104.                     });
  105.                 }
  106.                  
  107.             });
  108.              
  109.         });
  110.     });
  111.     // ]]>
  112. </script>
  113.     </head>
  114. <body>
  115. <?php $countries['#'] = 'Please Select'; ?>
  116.  
  117. <label for="country">Country: </label><?php echo form_dropdown('country_id', $countries, '#', 'id="country"'); ?><br />
  118.  <?php $cities['#'] = 'Please Select'; ?>
  119. <label for="city">City: </label><?php echo form_dropdown('city_id', $cities, '#', 'id="cities"'); ?><br />
  120. </body>
  121. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement