php无限极分类以及递归(thinkphp)

Posted 岁月如歌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php无限极分类以及递归(thinkphp)相关的知识,希望对你有一定的参考价值。

php无限极分类:

无限极分类重点在于表的设计:


1在model中:

class CatModel extends Model{
    protected $cat = array();
    public function __construct(){
        parent::__construct();
        $this->cats = $this->select();
    }
public function getTree($parent_id=0,$lev=0){
        $tree = array();
        foreach($this->cats as $c){
            if ($c[\'parent_id\'] == $parent_id) {
                $c[\'lev\'] = $lev;
                $tree[] = $c;
                //再查出下层的目录拼接到一起去
                $tree = array_merge($tree,$this->getTree($c[\'cat_id\'] , $lev+1));
            }
        }
        return $tree;
    }
}

 

2在controller中:

class GoodsController extends Controller{
public function catelist(){
        $catModel = D(\'Cat\');
        $catlist = $catModel->getTree();
        //print_r($catlist);
        $this->assign(\'list\',$catlist);
        $this->display();
    }
}

 

3在view中:

<!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> 
  <title>管理中心 - 商品分类 </title> 
  <meta name="robots" content="noindex, nofollow" /> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <link href="__PUBLIC__/Admin/styles/general.css" rel="stylesheet" type="text/css" /> 
  <link href="__PUBLIC__/Admin/styles/main.css" rel="stylesheet" type="text/css" /> 
 </head> 
 <body> 
  <h1> <span class="action-span"><a href="cateadd.html">添加分类</a></span> <span class="action-span1"><a href="#">管理中心</a> </span><span id="search_id" class="action-span1"> - 商品分类 </span> 
   <div style="clear:both"></div> </h1> 
  <form method="post" action="" name="listForm"> 
   <div class="list-div" id="listDiv"> 
    <table width="100%" cellspacing="1" cellpadding="2" id="list-table"> 
     <tbody>
      <tr> 
       <th>分类名称</th> 
       <th>商品数量</th> 
       <th>数量单位</th> 
       <th>导航栏</th> 
       <th>是否显示</th> 
       <th>价格分级</th> 
       <th>排序</th> 
       <th>操作</th> 
      </tr> 
    
      <foreach name=\'catlist\' item=\'cal\'>
      <tr align="center" class="0" id="tr_2"> 
       <td align="left" class="first-cell" style="padding-left="1""> <img src="__PUBLIC__/Admin/images/menu_minus.gif" id="icon_0_1" width="9" height="9" border="0" style="margin-left:{$cal[\'lev\']*2}em" /> <span><a href="#">{$cal[\'cat_name\']}</a></span> </td> 
       <td width="10%">0</td> 
       <td width="10%"><span>    </span></td> 
       <td width="10%"><img src="__PUBLIC__/Admin/images/no.gif" /></td> 
       <td width="10%"><img src="__PUBLIC__/Admin/images/yes.gif" /></td> 
       <td><span>0</span></td>
       <td width="10%" align="right"><span>50</span></td> 
       <td width="24%" align="center"><a href="{:U(\'admin/cat/catedit\',array(\'cat_id\'=>$cal[\'cat_id\']))}">编辑</a> | <a href="{:U(\'admin/cat/catdel\',array(\'cat_id\'=>$cal[\'cat_id\']))}" title="移除">移除</a> </td> 
      </tr> 
     </foreach>
     
     </tbody>
    </table> 
   </div> 
  </form> 
 <include file=\'./Index:footer\' /> 
 </body>
</html>

效果如下:

 

以上是关于php无限极分类以及递归(thinkphp)的主要内容,如果未能解决你的问题,请参考以下文章

php无限极分类实现的方法?

php 非递归实现分类树

无限极分类不用递归生成多维数组(树)的方法

php之利用递归写无限极分类

thinkphp无限极分类

php 无限极递归分类