Home  >  Article  >  php教程  >  ci框架 省市三级联动

ci框架 省市三级联动

PHP中文网
PHP中文网Original
2016-05-25 17:08:291571browse

ci框架 省市三级联动           

只要在ci中引入这个就可以了简单

<?php             
$data[&#39;province_selected&#39;] = $address[&#39;province_id&#39;];             
$data[&#39;city_selected&#39;] = $address[&#39;city_id&#39;];             
$data[&#39;district_selected&#39;] = $address[&#39;district_id&#39;];             
$this->load->view(&#39;district_select&#39;,$data);   
 ?>

1. [文件] district_select.php

<?php
$CI = get_instance();
$CI->load->model(&#39;region_model&#39;, &#39;region&#39;);
$provinces   = $CI->region->provinces();
$citys = $CI->region->children_of($province_selected);
?>
<script  language="JavaScript">
 

<?php if(isset($province_selected)):?>
var province_selected = <?php echo (int)$province_selected?>;
<?php else:?>
var province_selected = 0;
<?php endif?>

<?php if(isset($city_selected)):?>
var city_selected = <?php echo (int)$city_selected?>;
<?php else:?>
var city_selected = 0;
<?php endif?>

<?php if(isset($district_selected)):?>
var district_selected = <?php echo (int)$district_selected?>;
<?php else:?>
var district_selected = 0;
<?php endif?>

$(document).ready(function() {

  var change_city = function(){
	$.ajax({
	  url: &#39;<?php echo  ITURL.&#39;/admin.php/region_change/select_children/parent_id&#39;
	  // site_url(&#39;region_change/select_children/parent_id&#39;)?>&#39;+&#39;/&#39;+$(&#39;#province_id&#39;).val(),
	  type: &#39;GET&#39;,
	  dataType: &#39;html&#39;,
      success: function(data){
		city_json = eval(&#39;(&#39;+data+&#39;)&#39;);
		var city = document.getElementById(&#39;city_id&#39;);
		city.options.length = 0;
		city.options[0] = new Option(&#39;城市&#39;, &#39;-11&#39;);
		for(var i=0; i<city_json.length; i++){
            var len = city.length;
			city.options[len] = new Option(city_json[i].region_name, city_json[i].region_id); 
			if (city.options[len].value == city_selected){
				city.options[len].selected = true;
			}
		}
		change_district();//重置地区
	  }
	});
  }

  change_city();//初始化城市

  $(&#39;#province_id&#39;).change(function(){
     change_city();
  });


  var change_district = function(){
	$.ajax({
     url: &#39;<?php echo  ITURL.&#39;/admin.php/region_change/select_children/parent_id&#39;
     //site_url(&#39;region_change/select_children/parent_id&#39;)?>&#39;+&#39;/&#39;+$(&#39;#city_id&#39;).val(),
	  type: &#39;GET&#39;,
	  dataType: &#39;html&#39;,
	  success: function(data){
        district_json = eval(&#39;(&#39;+data+&#39;)&#39;);
		var district = document.getElementById(&#39;district_id&#39;);
		district.options.length = 0;
		district.options[0] = new Option(&#39;县/区&#39;, &#39;-22&#39;);
		for(var i=0; i<district_json.length; i++){
            var len = district.length;
	district.options[len] = new Option(district_json[i].region_name, district_json[i].region_id); 
			if (district.options[len].value == district_selected){
				district.options[len].selected = true;
			}
		}
	  }
	});
  }

  $(&#39;#city_id&#39;).change(function(){
     change_district();
  });
  


});

 
</script>
<select name="province_id" id="province_id"  style="width:100px;">
    <option value="-1" >省份</option>
	<?php foreach($provinces as $key => $province): ?>
	<option value="<?php echo $province[&#39;region_id&#39;]; ?>" 
	<?php if($province[&#39;region_id&#39;]==$province_selected){echo &#39;selected&#39;;}?> >
	<?php echo $province[&#39;region_name&#39;]; ?>
	</option>
	<?php endforeach; ?>

</select>

<select name="city_id" id="city_id"  style="width:100px;">
    
</select>

<select name="district_id" id="district_id" style="width:100px;">
    <option value=""></option>
</select>

2. [文件] region_model.php

<?php
/**
 * 省市县
 *
 *
 */
class Region_Model extends CI_Model
{
    /**
     * 
     *
     * @return Region_Model
     */
    function Region_Model()
    {
        parent::__construct();
    }
    
	// --------------------------------------------------------------------

    /**
     * 
     *
     * @param integer $parent_id
     */
    function children_of($parent_id, $select="*")
    {
        $parent_id = (int)$parent_id;
        
        $regions = array();
        $this->db->select($select);
        $this->db->where(&#39;parent_id&#39;, $parent_id);
        if ($query = $this->db->get(&#39;region&#39;)){
            return $query->result_array(); 
		}
		return array();       
    }

    // --------------------------------------------------------------------

	/**
     * 
     *
     * @return array
     */
    function provinces()
    {
        return $this->children_of(1);
    }

    // --------------------------------------------------------------------

    /**
	 * 区域名
	 *
	 *
	 */	
    function get_name($id)
	{
		if (!$id){
            return array();
        }
		$this->db->select(&#39;region_name&#39;);
        $query = $this->db->get_where(&#39;region&#39;,array(&#39;region_id&#39; => $id));

        if ($row = $query->row_array()){
            return $row[&#39;region_name&#39;];
        }
		return array();
	}

   // --------------------------------------------------------------------

    /**
	 * load by id
	 *
	 *
	 */	
	function load($id)
	{
        if (!$id){
            return array();
        }
        $query = $this->db->get_where(&#39;region&#39;,array(&#39;region_id&#39; => $id));
        if ($row = $query->row_array()){
            return $row;
        }
		return array();
	}

}

 以上就是ci框架 省市三级联动的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn