有空的时候具体把从数据库声明,到最后添加删除整体写个出来吧!
-=======================
<?php
/***************************************************************
**界面:无限分类界面
**时间:2011-6-12
**文件名:category.class.php
***************************************************************/
class Category extends Action{
function index(){
$list=$this->catlist();
$this->assign("cat",$list);
$this->display();
}
function catlist(){
$cat=D("category");
$list=$cat->field("id,fid,name,path,concat(path,'-',id) as bpath")->order('bpath')->select();//查询
foreach($list as $key=>$value){
$list[$key]["name"]=str_repeat(' ',count(explode('-',$list[$key]['bpath']))-2).'|-'.$list[$key]['name'];
}
return $list;
}
/*
* 添加分类:
* str_repeat:重复count得到的值 -> str_repeat("填充值",int())
* explode('-',$list[$key]['bpath']):按照 '-' 分割$list[$key]['bpath']
* 最后使用 |- 连接分类名
*/
function addcat(){
$cat=D("category");
$list=$cat->field("id,fid,name,path,concat(path,'-',id) as bpath")->order('bpath')->select();
foreach($list as $key=>$value){
$list[$key]["name"]=str_repeat(' ',count(explode('-',$list[$key]['bpath']))-2).'|-'.$list[$key]['name']; //遍历
}
$this->assign("cate",$list);
$this->display();
}
function insert(){
$cat=D("category");
$fid=isset($_POST['fid'])?(int)$_POST['fid']:0; //判断fid值是否为0,为0时是顶级分类
$list=$cat->where("id=$fid")->find(); //查询出POST内传过来的fid值
if($fid==0){
$path=0-$fid; //返回path值
}else{
$path=$list['path'].'-'.$list['id']; //定义bpath,使原有path和新id连接组成新path
}
$_POST['path']=$path;
if(!empty($_POST["name"])){
if($cat->insert()){
$this->success("添加分类成功",1,"index");
}else{
$this->error("添加分类失败",1,"addcat");
}
}else{
$this->error("分类名不能为空");
}
}
//删除分类,如有子分类提示
function delcat(){
$cat = D("category");
$id=$_GET["id"];
$c=$cat->where("fid=$id")->select();
P($id);
P($c);
if($id!==$c[0]["fid"]){
if($cat->delete($id)){
$this->success("删除分类成功",1,"index");
}else{
$this->error("删除分类失败",2,"index");
}
}else if($id==$c[0]["fid"]){
$this->error("分类下面有子分类,请删除!",2,"index");
}else if($c[0]["fid"]==0){
if($c->delete($_GET["id"])){
$this->success("删除分类成功",1,"index");
}else{
$this->error("删除分类失败",2,"index");
}
}
}
}