Thinkphp in provincial and municipal cascade drop-down list

Source: Internet
Author: User

Public Function Drop Location Common folder under Common.php file (this code can also be placed in the controller to be used)

Encapsulated drop-down list function code:

/** * Assemble a drop-down list from the list ADD by CK * @param $list: Data source * @param $value: Value value of option displayed, subscript example: ID in $list [' ID '] * @param $text: Example of the text value of option displayed: $list name in [' name '] @param int $selectIndex: Checked index entry * @param bool $haveFirst : Is there a first item such as ' <option value= ' 0 > Please select </option> ', default = False * @return bool|string*/    functionGetselectoption ($list,$value,$text,$selectIndex= 0,$haveFirst=false)    {   if(!Is_array($list) )    {        return false; }        if(!isset($list)||!isset($value)||!isset($text))        {            return false; }        if(Empty($list)||Empty($value)||Empty($text))        {            return false; }        if(!$haveFirst)//If there is a first item, do not add        {            $resultStr= ' <option value= ' 0 ' > Please select </option> '; }        foreach($list  as $option)        {            if($option[$value] ==$selectIndex ){                $resultStr. = ' <option value= '.$option[$value]. ' selected= ' selected ' > '.$option[$text].‘ </option> '; }Else            {                $resultStr. = ' <option value= '.$option[$value].‘ > '.$option[$text].‘ </option> '; }        }        return $resultStr; }

Exchange Controller code:

 Public function index () {$thisarray$this// page initialization gets province.  $thisdisplay ();}
/** * Get provinces and cities * @param type $pid * @param type $ajax ajax call * @return Type array*/     Public functionGetregion ($pid= 0,$ajax=false,$checked= 0,$prvcheck=false) {        $r= D ("region")->where ("parentid=").$pid),Select (); Region provinces and cities cascading tables, ParentID father-level id,pid self-increment IDif($ajax) {            $this->ajaxreturn (Getselectoption ($r, ' id ', ' name ')); } Else {            if($prvcheck) {                $prvid= D ("region")->query ("Select-ID from-hengtu_region where id= (select ParentID from hengtu_region where parentid= (select ParentID from Hengtu_region where id= ".$pid. ") Limit 1)"); returnGetselectoption (D ("Region")->where ("Parentid=0")->select (), ' id ', ' name ',$prvid[0] [' ID ']); }            returnGetselectoption ($r, ' id ', ' name ',$checked); }    }

On the index page you want to display the location of the provinces and cities Cascade:

<Fontstyle= "FONT-SIZE:12PX;">Region selection:</Font>      <SelectID= "Addarea_sheng"name= ' Cityid ';>{$option. Prov}</Select>      <SelectID= "Addarea_shi"name= ' Cityid 'style= "Display:none;" >              </Select>      <SelectID= "Addarea_quxian"name= ' Cityid 'style= "Display:none;" >                           </Select> <Script>       $("#addarea_sheng"). Change (function() {            varProvid= $("#addarea_sheng"). Val (); $.get ("{: U (' exchange/getregion ')}?pid=" +Provid+ "&ajax=1", function(data) {$ ("#addarea_shi"). Show (0, function() {                      $( This). HTML (data);            }); })       })       $("#addarea_shi"). Change (function() {              varProvid= $("#addarea_shi"). Val (); $.get ("{: U (' exchange/getregion ')}?pid=" +Provid+ "&ajax=1", function(data) {$ ("#addarea_quxian"). Show (0, function() {                           $( This). HTML (data);              }); })        })</Script>

Thinkphp in provincial and municipal cascade drop-down list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.