咨询电话:186 7916 6165 咨询电话:186 7916 6165 (微信同号)    在线QQ:181796286
NEWS BLOG ·
学无止境
关注开优网络 关注前沿
PHP Web开发之权限模块
PHP Web开发之上传图片

PHP Web开发之无限级分类

发表日期:2015-10-26    文章编辑:南昌开优网络    浏览次数:3088    标签:PHP应用

ThinkPHP中的无限级分类
模版页category.html
<script type="text/javascript" src="__PUBLIC__/js/jquery.js"></script>
<script type="text/javascript">
function addclick() {
frm.action = "__URL__/add";//添加事件
}
function editclick() {
frm.action = "__URL__/edit";//修改事件
}
function delclick() {
frm.action = "__URL__/del";//删除事件
}
 
function isSelected(pid){
var caname=$.trim(pid.options[pid.selectedIndex].text);//获取下拉列表选中的文本去掉前后空格
document.getElementById("txtmodcaname").value = caname;
document.getElementById("txtdelcaname").value = caname;
document.getElementById("hfid").value = pid.options[pid.selectedIndex].value; 
}
</script>
</head>
<body>
<form id="frm" name="frm" method="post" action="">
    <select onchange="isSelected(this);" name="pid" size="20" id="pid">
      <option>分类列表</option>
      <volist name="alist" id="vo">
        <option value="<{$vo['id']}>">
        <php>        
       for($i=0;$i<$vo['count'];$i++){
       echo "&nbsp;";
       }
        </php>       
        <{$vo['caname']}></option>
      </volist>
    </select>
    <input type="text" name="txtaddcaname" id="txtaddcaname" /> 
    <input name="hfid" type="hidden" id="hfid" />
    <input name="btnAdd" type="submit" onclick="addclick();" id="btnAdd" value="添加" /> 
  <input type="text" name="txtmodcaname" id="txtmodcaname" /> 
  <input type="submit" onclick="editclick();" name="btnMod" id="btnMod" value="修改" /> 
    <input type="text" name="txtdelcaname" id="txtdelcaname" />
    <input type="submit" onclick="delclick();" name="btnDel" id="btnDel" value="删除" />
</form>

控制器
<?php
header ( "Content-Type: text/html;charset=utf-8" );
class CatAction extends Action {
//页面加载
public function index() {
$cat = new Model ( 'category' );
//list=$cat->query("select * from shop_category order by code asc");
$list = $cat->order('code asc')->select ();
foreach($list as $key=>$value){//key为下标,value为值
$list[$key]['count']=count(explode('0',$value['code']));//explode为分割
}
$this->assign ( 'alist', $list );//分配变量
$this->display ( './Public/category.html' );
}
//添加
public function add() {
$cat = new CatModel ( 'category' );
$caname = trim ( $_POST ['txtaddcaname'] );
if (strlen ( $caname ) == 0) {
echo "<script>alert('请输入分类名称');history.back();</script>";
exit ();
}
$pid=isset($_POST['pid'])?(int)$_POST['pid']:0;//获取pid值
$code = $cat->GenCode ( $pid );//通过传入的pid生成code
$data ['caname'] = $caname;
$data ['pid'] = $pid;
$data ['code'] = $code;
$list = $cat->add ( $data );
if ($list > 0) {
echo "<script>alert('添加成功');history.back();</script>";
exit ();
} else {
echo "<script>alert('添加失败');history.back();</script>";
exit ();
}
}
//修改
public function edit() {
$cat = new Model ( 'category' );
$caname=trim($_POST['txtmodcaname']);
if(strlen($caname)==0){
echo "<script>alert('请输入分类名称');history.back();</script>";
exit ();
}
$id=$_POST['hfid'];
$data['caname']=$caname;
$list=$cat->where("id=$id")->save($data);
if($list>0){
echo "<script>alert('修改成功');history.back();</script>";
exit ();
}else{
echo "<script>alert('修改失败');history.back();</script>";
exit ();
}
}
//删除
public function del() {
$cat = new Model ( 'category' );
$caname=trim($_POST['txtdelcaname']);
if(strlen($caname)==0){
echo "<script>alert('请选择分类名称');history.back();</script>";
exit ();
}
$id=$_POST['hfid'];
$list=$cat->where("id=$id")->delete();
if($list>0){
echo "<script>alert('删除成功');history.back();</script>";
exit ();
}else{
echo "<script>alert('删除失败');history.back();</script>";
exit ();
}
}
}
?>

模型
<?php
header ( "Content-Type: text/html;charset=utf-8" );
class CatModel extends Model{
public function GenCode($pid){
$list=$this->where("pid=$pid")->field("max(code)as bh")-> find();
//$list=$this->query("select max(code) from shop_category where pid=$pid");
if($list["bh"]==""||$list["bh"]==null){
if($pid=="0"){
return "01";
}else{
$tmp1=$this->where("id=$pid")->field('code')-> find();
//$tmp=$this->query("select code from shop_category where id=$pid");
return $tmp1['code']."01";
}
}else{
$tmp2=$this->where("pid=$pid")->field("max(code)as bh")-> find();
//$tmp=$this->query("select max(code) from shop_category where pid=$pid");
$left=substr($tmp2['bh'],0,strlen($tmp2['bh'])-1);
$right=substr($tmp2['bh'],strlen($tmp2['bh'])-1)+1;
return $left.$right;
}
}
}
?>