无限级分类

浏览:982 发布日期:2014/11/06 分类:求助交流 关键字: 无限级分类
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="__PUBLIC__Css/public.css" />
</head>

<body>
    <form action="{:U(GROUP_NAME.'/Cate/runCate')}" method="post">
    <table class="Tab">
        <tr>
            <th colspan="2" class="cen">添加栏目分类</th>        
        </tr>
        <tr>
            <td align="right">所属栏目</td>
            <td>
                <select name="pid">
              <option value="0">顶级栏目</option>
               <?php foreach ($cates as $k => $v):?>
               <option value="<?php echo $v['id'];?>">
               <?php echo   $v['name']; ?>  <?php echo $v['id'];?>
              </option>
               <?php endforeach; ?>
               </select>
               </td>
        </tr>
        <tr>
            <td align="right">栏目分类名称</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td align="right">排序</td>
            <td><input type="text" name="sort" value="100"></td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="hidden" name="pid"  value="{$pid}"/>
                <input type="submit" value="保存" class="bt3" />
            </td>
            
        </tr>
    </table>
    </form>
</body>
</html>
//表单处理
    public function runCate(){
        p($_POST);die;
        if(M('cate')->add($_POST))
        {
            $this->success('添加成功',U(GROUP_NAME.'/Cate/index'));
        }
        else
            {
                $this->error('添加失败');
            
            }
        
    }
//无限级分类<?php
class category {
    Static public function Leveler ($cate,$html='--',$pid=0,$level=0){
        $arr=array();
        foreach ($cate as $v){
            if($v['pid']==$pid){
                $v['level']=$level + 1;
                //echo $v['level'];
                $v['html']=str_repeat($html,$level);
                $arr[]=$v;
            $arr=array_merge($arr, self::Leveler ($cate,$html,$v['id'],$level + 1));
//                foreach($v as $key=>$value)
//               echo ' '.$key."=>".$value;
//                echo '<br/>';
            }
            
        }
        return $arr;
    }    
        
}
?>
结果:为什么”pid=0“呢?
最佳答案
评论( 相关
后面还有条评论,点击查看>>