條件,數據庫裡分類是按id,fid(父ID)實現多級分類的!
使用方法:
代碼如下復制代碼$sql="XXXXXXXXXX"; //sql語句
$res=$db->Select($sql); //執行sql
$list=array();
treeList(treeGet($res),$list); /生成樹
print_r($res); //打印出來看看!
使用結果:
┣推薦新聞啊
┃┣國際新聞
┃┣dfffffg
┃┣ttttttt
┃┃┗yyyyy
代碼如下:
代碼如下復制代碼/**
* 選擇SQL涵數
*
* @access public
* @param Array $field 字段信息,支持涵數
* @param Array $table 數據庫表
* @param Array $where 條件
* @return SQL SQL語句
*/
functiontreeGet($data)
{
$tmptree=null;
$tree=$data;
returntreeAddNodeToTree($tmptree,treegetbyuid($tree,0,@$field),$tree);
}
/**
*插入SQL涵數
*
* @access public
* @param Array $fieldResult 字段信息,支持涵數
* @param Array $table 數據庫表
* @return SQL SQL語句
*/
functiontreeAddNodeToTree($Node,$miniTree,&$source)
{
if(is_array($miniTree)) {
foreach($miniTreeas$k=>$v)
{
if(!count($miniTree[$k]['child']=treeAddNodeToTree($miniTree[$k],treegetbyuid($source,@$v['id']),$source)))
{
unset($miniTree[$k]['child']);
$miniTree[$k]['leaf']=true;//設置葉結點
}
}
return$Node['child']=$miniTree;
}
}
functiontreegetbyuid(&$stree,$uid)
{
$dtree=array();
if(is_array($stree)){
foreach($streeas$k=>$v)
{
if($v['fid']==$uid)
{
$mytmp=array();
$mytmp=$v;
unset($stree[$k]);
array_push($dtree,$mytmp);
$mytmp=null;
}
}
}
return$dtree;
}
/**
*更新SQL涵數
*
* @access public
* @param Array $fieldResult 字段信息,支持涵數
* @param Array $table 數據庫表
* @param Array $where 條件
* @return SQL SQL語句
*/
functiontreeMakeDeep($deep)
{
$returnValue="";
for(;$deep;$deep--)
{
$returnValue.="┃";
}
return$returnValue."┣";
}
functiontreeList($treeData,&$List)
{
static$deep=0;
if(is_array($treeData))
{
foreach($treeDataas$k=>$v)
{
$v['deepValue']=treeMakeDeep($deep);
$v['deep']=$deep;
$t=$v;
unset($t['child']);
array_push($List,$t);
if($v['child'])
{
++$deep;
$optionsNode.=treeList($v['child'],$List);
$deep--;
}
}
if($lastV=array_pop($List))
{
$lastV['deepValue']=str_replace('┣','┗',$lastV['deepValue']);
array_push($List,$lastV);
}
}
}
functiontreeSelect($tree,$id,$options="child")
{
switch(strtolower($options))
{
case"child":
$tmpTree=array();
$deep=-1;
foreach($treeas$k=>$v)
{
if($id==$v['id'])
{
array_push($tmpTree,$v);
$deep=$v['deep'];
}elseif($deep!=-1)
{
if($v['deep']<=$deep)
{
break;
}else
{
array_push($tmpTree,$v);
}
}
}
break;
case"remove":
default:
$tmpTree=array();
$deep=-1;
foreach($treeas$k=>$v)
{
if($id==$v['id'])
{
$deep=$v['deep'];
continue;
}elseif($deep!=-1)
{
if($v['deep']<=$deep)
{
array_push($tmpTree,$v);
$deep=-1;
}
continue;
}
array_push($tmpTree,$v);
}
}
return$tmpTree;
}