<?php
//wolf qq116311316 2010/6/12
//数据已经循环出来了,接下来的操作 删除等等 就不用我说了吧 O(∩_∩)O~
//测试数据
$cat_list = array(
array('id' => 1, 'f_id' => 0, 'name' => '名称1', ),
array('id' => 2, 'f_id' => 0, 'name' => '名称2',),
array('id' => 3, 'f_id' => 0, 'name' => '名称3',),
array('id' => 4, 'f_id' => 1, 'name' => '名称4',),
array('id' => 5, 'f_id' => 4, 'name' => '名称5',),
array('id' => 6, 'f_id' => 2, 'name' => '名称6', ),
array('id' => 7, 'f_id' => 3, 'name' => '名称7', ),
array('id' => 8, 'f_id' => 0, 'name' => '名称8',),
);
//循环分类
function tree($sorts,$selected=0,$f_id=0,$leven=0)
    {
         static $list;//这个一定要有 不然不能赋值的
     
        foreach ($sorts as $sort)   //循环数组
        {
            if ($sort['f_id'] == $f_id)   //如果是同组
            {
                  if($sort['id'] == $selected)    //如果ID等于要选中的编号
                {
                    $list.='<option value="'.$sort['id'].'"selected="selected">'.str_repeat('|-',$leven).$sort['name'].'</option>'."\r\n";
                }else{
                  $list.= '<option value="'.$sort['id'].'">'.str_repeat('|-',$leven).$sort['name'].'</option>'."\r\n";
                /** 查找属于自己的下一级,调用自身 */       
                tree($sorts,$selected,$sort['id'], $leven+1);
           
                }
            }           
        }
     
         return $list;
    }
echo "<select name='fid'>".tree($cat_list,3)."</select>";
?>

 

 

function arrcategory($data,$pid='0',$count){
 $arr=array();
 foreach ($data as $k=>$v){
     if($v['parent_id']==$pid){
      $res['cat_name']=$v['cat_name'];
   $v['count']=$count;
   //$arr.=$v['cat_name']."<br/>";
   $arr[]=$v;
   $v['child']=arrcategory($data,$v['cat_id'],$count+1);
   
   
  }
        //print_r($v);
  //echo($v['parent_id'])."<br/>";
 }
 return $arr;
}

posted on 2014-01-17 12:00  大海航手  阅读(167)  评论(0编辑  收藏  举报