search
Homephp教程PHP源码ci框架 省市三级联动

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool