thinkphp 地区管理功能,因为最近有些系统有这方面的需求行业不一样用到区域也不一样,完成70%, ajax 双击修改地区部分没实现,有时间弄下。本来打算移植ecshop 的不过有点麻烦。还是自己写把。数据库部分来自ecshop region表。至于前端部分,参考任何一个城市级联即可实现,高端上档次!
实现效果:
前台代码:
<form name="form1" method="post" id="myfrom" action="__URL__/add">
<table class="table_form" style="border:0;">
<tr>
<th align="left">
{if $region_type=3}{$regionts}:
<input type="text" class="text-input" name="region_name" >
<input type="hidden" name="region_type" value="{$region_type}" />
<input type="hidden" name="parent_id" value="{$Id}" />
<input type="submit" class="buttonAdd" value="提交操作"/>
{/if}
<a href="{:U('Association/region',array("regiontype"=>$region_typeyuanshi,'parentid'=>$fuID))}">返回上一级</a>
</th>
</tr>
</table>
</form>
<div class="fl" style="border:1px solid #66CCFF;">
<table width="100%" border="0" cellpadding="0" cellspacing="0" >
<tr><td height="15" style="background:#66CCFF; margin-top:0px; ">{$region_type}</td></tr>
<tr bgcolor="#FFFFFF">
<td>
<present name="list">
<volist name="list" id="vo">
<div class='regionys'><span id="regionname{$vo['Id']}">{$vo['region_name']}</span><span>|</span>
{if $region_type=3}
<a href="{:U('Association/region',array("regiontype"=>$region_type,'parentid'=>$vo['Id']))}">管理</a>
{/if}
<a href="{:U('Association/delRegion',array("regionid"=>$vo['Id'],'parentid'=>$vo['Id']))}" >删除</a></div>
</volist>
<else />
<div class='regionys'>暂无地区</div>
</present>
</td>
</tr>
<tr><td height="5" style="background:#fff; margin-top:0px; "></td></tr>
</table>
</div>
<p class="i">
<font color="red">注视:</font>1级地区:国家 2级地区:省份 3级地区:市级 4级地区:县/区
</p>
public function region(){
$parent_id = intval(trim($_GET['parentid']));//父ID
$region = D("region");
$regionarr = $region->where(" parent_id=".$parent_id)->select();
if( $_GET['regiontype']=="" ){
$region_type = '0';
$regionts = "新增1级地区";
}else{
if($regionarr[0]['Id'] == ""){
//取出上一级的region_type
$regiontypearr = $region->where('id='.$parent_id)->field('region_type')->find();
$region_type = $regiontypearr['region_type']+1;
}else{
$region_type = $regionarr[0]['region_type'];
}
$regionts = $region_type+1;
$regionts = "新增".$regionts."级地区";
}
$this->assign("region_type",$region_type);//属于省份还是市
$region_typeyuanshi=$region_type-1;
$this->assign("region_typeyuanshi",$region_typeyuanshi);
$this->assign("regionts",$regionts);//文字提示
if( $regionarr[0]['parent_id'] != 0){
$fuidarr = $region->where("id=".$regionarr[0]['parent_id'])->find();//用fuID去取fuID的parent_id;
$this->assign("fuID", $fuidarr['parent_id'] );//返回上一级的时候,取的fuID
}
if( $region_type == 0 ){
$this->assign('Id', '0' );
}else{
$this->assign('Id', $parent_id );
}
$this->assign('list',$regionarr);
$this->display();
}
function delRegion(){
$region = D("Region");
//删除
if( $_GET['regionid'] != "" ){
//先判断是否有子分类
$regionid= intval( $_GET['regionid'] );
$ziarr = $region->where("parent_id=".$regionid)->select();
if( $ziarr[0]['Id'] != "" ){
$this->error("请先删除子分类!");
}else{
$region->Id=$regionid;
if($region->delete()){
$this->success();
}else{
$this->error("删除失败");
}
}
}
}
public function add(){
$Region = D('Region');
$region_name=trim($_POST['region_name']);
if(!$region_name) $this->error("新增地区不能为空!");
$Region->region_name = $region_name;
$Region->region_type = trim($_POST['region_type']);
$Region->parent_id = trim($_POST['parent_id']);
if($Region->add()){
$this->success('添加成功!');
}else{
$this->error($Region->getError());
}
}
数据库结构:
CREATE TABLE `sp_region` (
`Id` INT(11) NOT NULL AUTO_INCREMENT,
`parent_id` INT(11) DEFAULT NULL,
`region_name` VARCHAR(120) CHARACTER SET gbk DEFAULT NULL,
`region_type` SMALLINT(5) DEFAULT NULL,
`agency_id` SMALLINT(5) UNSIGNED NOT NULL DEFAULT '0',
`is_show` SMALLINT(5) NOT NULL DEFAULT '0',
PRIMARY KEY (`Id`)
) ENGINE=MYISAM AUTO_INCREMENT=3417 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
数据的话,ecshop 里面region 表和其一样。