ci框架 省市三级联动
ci框架 省市三级联动
只要在ci中引入这个就可以了简单
<?php $data['province_selected'] = $address['province_id']; $data['city_selected'] = $address['city_id']; $data['district_selected'] = $address['district_id']; $this->load->view('district_select',$data); ?>
1. [文件] district_select.php
<?php $CI = get_instance(); $CI->load->model('region_model', 'region'); $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: '<?php echo ITURL.'/admin.php/region_change/select_children/parent_id' // site_url('region_change/select_children/parent_id')?>'+'/'+$('#province_id').val(), type: 'GET', dataType: 'html', success: function(data){ city_json = eval('('+data+')'); var city = document.getElementById('city_id'); city.options.length = 0; city.options[0] = new Option('城市', '-11'); 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();//初始化城市 $('#province_id').change(function(){ change_city(); }); var change_district = function(){ $.ajax({ url: '<?php echo ITURL.'/admin.php/region_change/select_children/parent_id' //site_url('region_change/select_children/parent_id')?>'+'/'+$('#city_id').val(), type: 'GET', dataType: 'html', success: function(data){ district_json = eval('('+data+')'); var district = document.getElementById('district_id'); district.options.length = 0; district.options[0] = new Option('县/区', '-22'); 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; } } } }); } $('#city_id').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['region_id']; ?>" <?php if($province['region_id']==$province_selected){echo 'selected';}?> > <?php echo $province['region_name']; ?> </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('parent_id', $parent_id); if ($query = $this->db->get('region')){ 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('region_name'); $query = $this->db->get_where('region',array('region_id' => $id)); if ($row = $query->row_array()){ return $row['region_name']; } return array(); } // -------------------------------------------------------------------- /** * load by id * * */ function load($id) { if (!$id){ return array(); } $query = $this->db->get_where('region',array('region_id' => $id)); if ($row = $query->row_array()){ return $row; } return array(); } }
以上就是ci框架 省市三级联动的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



With the development of network technology, PHP has become one of the important tools for Web development. One of the popular PHP frameworks - CodeIgniter (hereinafter referred to as CI) has also received more and more attention and use. Today, we will take a look at how to use the CI framework. 1. Install the CI framework First, we need to download the CI framework and install it. Download the latest version of the CI framework compressed package from CI's official website (https://codeigniter.com/). After the download is complete, unzip

PHP is a popular programming language that is widely used in web development. The CI (CodeIgniter) framework is one of the most popular frameworks in PHP. It provides a complete set of ready-made tools and function libraries, as well as some popular design patterns, allowing developers to develop Web applications more efficiently. This article will introduce the basic steps and methods of developing PHP applications using the CI framework. Understand the basic concepts and structures of the CI framework. Before using the CI framework, we need to understand some basic concepts and structures. Down

PHP is a widely used server-side scripting language, and CodeIgniter4 (CI4) is a popular PHP framework that provides a fast and excellent way to build web applications. In this article, we will get you started using the CI4 framework to develop outstanding web applications by walking you through how to use it. 1. Download and install CI4 First, you need to download it from the official website (https://codeigniter.com/downloa

With the development of the Internet and its continuous integration into people's lives, the development of network applications has become more and more important. As a well-known programming language, PHP has become one of the preferred languages for developing Internet applications. Developers can use numerous PHP frameworks to simplify the development process, one of the most popular is the CodeIgniter (CI) framework. CI is a powerful PHP web application framework. It has the characteristics of lightweight, easy to use, optimized performance, etc., allowing developers to quickly build

The steps to introduce CSS styles in the CI framework are as follows: 1. Prepare CSS files; 2. Store the CSS files in the appropriate location of the CI framework project; 3. In the pages that need to use CSS styles, introduce CSS through the HTML <link> tag File; 4. Use the CSS class or ID name in the HTML element to apply the corresponding style.

Tutorial: Detailed steps for introducing CSS styles in the CI framework, specific code examples are required Introduction: Style is a crucial part of developing web applications. Use CSS (Cascading Style Sheets) to beautify web pages and provide a better user experience. When developing using the CodeIgniter (CI) framework, how to correctly introduce CSS styles is particularly important. This article will introduce the detailed steps of introducing CSS styles in the CI framework and provide you with specific code examples. Step 1: Create CSS File First,

The steps for introducing CSS styles in the CI framework require specific code examples. The CI (CodeIgniter) framework is a popular PHP development framework that is widely used to build efficient web applications. When developing web applications, a beautiful user interface is an important consideration. Using CSS styles can optimize and personalize the web application interface, giving users a better experience. In a CI framework, introducing CSS styles usually requires the following steps, accompanied by specific code examples. step 1:

PHP is a very popular web development language, and CodeIgniter (CI) is a very popular PHP framework. CodeIgniter provides many useful functions and features, bringing great convenience to developers. In this article, we will explore how to use the CI6 framework. Installing CI6 Before you can start using CI6, you must first complete the installation process. You need to first download the CI6 compressed package from the CodeIgniter official website. Then, unzip this file and place it in
