這個是經過本人實踐確實可以使用的。
先說下表結構。一共三個字段iClassID,iParentID,cClassName;
一個是分類的id,一個是父id,一個是分類的名字,
下面是代碼:
最終效果如圖:
先說下表結構。一共三個字段iClassID,iParentID,cClassName;
一個是分類的id,一個是父id,一個是分類的名字,
下面是代碼:
<?php
class Tree
{
/*
*在這里需要注意的是,類需要傳入一個數組。即分類的全部數據,是一個二維的數組
*
*/
//分層
private $view;
//private $path;
private $data=array();//id=>信息
private $cateArray=array();//id=>pid
public function __construct($dataArray)
{
foreach ($dataArray as $val)
{
$this->setNode($val['iClassID'], $val['iParentID'], $val['cClassName']);
}
$this->sortASC();
}
//設置樹的節點
function setNode($id, $parent, $value)
{
$parent = $parent?$parent:0;
$this->data[$id] = $value;
$this->cateArray[$id] = $parent;
}
/*
* 遞歸實現
* 得到id下的子樹結構數組(用多維數組格式來表示樹)
* id Internet 節點id (0表示的是根節點,是虛擬的,即沒有與它對應的實際信息)
* return array 子樹節點數組
*/
function getChildsTree($id=0)
{
$childs=array();
foreach ($this->cateArray as $child=>$parent)
{
if ($parent==$id)
{
$childs[$child]=$this->getChildsTree($child);
}
}
return $childs;
}
/*
* 遞歸實現
* 得到id節點的所有后代節點
* $id
* return array 索引=>id,...一維數組(順序important)
*/
function getChilds($id=0)
{
$childArray = array();
$childs = $this->getChild($id);
foreach ($childs as $child)
{
$childArray[]=$child;
$childArray=array_merge($childArray,$this->getChilds($child));
}
return $childArray;
}
/*
* 得到id節點的孩子節點
* $id
* return array 索引=>id,...一維數組
*/
function getChild($id)
{
$childs=array();
foreach ($this->cateArray as $child=>$parent)
{
if ($parent==$id)
{
$childs[]=$child;
}
}
return $childs;
}
/*
* 遞歸實現
* 反線獲得節點id的父節點
* $id interger 不可以為0
* return array 其父節點數組
*/
function getNodeLever($id)
{
$parents=array();
if (array_key_exists($this->cateArray[$id],$this->cateArray))//它的父節點,在節點樹中
{
$parents[]=$this->cateArray[$id];
$parents=array_merge($parents,$this->getNodeLever($this->cateArray[$id]));
}
return $parents;
}
/*
* 根據所在層數得到n-1個前導格式化符號
* $id Internet 不可以取0
* $preStr str 填充的格式化符號
* return str 多個格式化符號
*/
function getLayer($id,$preStr='|-')
{
return str_repeat($preStr,count($this->getNodeLever($id)));
}
//得到id節點的信息
function getValue ($id)
{
return $this->data[$id];
}
/*
* id降序
*/
function sortDES()
{
krsort($this->cateArray);
}
/*
* id升序
*/
function sortASC()
{
ksort($this->cateArray);
}
//下拉列表框樣式,
function select($cid = 0){
$category = $this->getChilds(0);
//file_put_contents('log.txt',var_export($category,true));
foreach ($category as $key=>$id)
{
if($cid == $id){
$this->view .= "<option value='$id' selected='selected'>".$this->getLayer($id, '|-').$this->getValue($id)."</option>";
}
else{
$this->view .= "<option value='$id'>".$this->getLayer($id, '|-').$this->getValue($id)."</option>";
}
}
return $this->view;
}
//表格樣式
function view()
{
$category = $this->getChilds(0);
foreach ($category as $key=>$id)
{
$this->view .= "<tr><td>".$this->getLayer($id, '|-').$this->getValue($id)."</td><td><a href='edit/id/$id'>編輯</a>|<a href='del/id/$id' onclick='if(confirm('是否確定刪除')==false)return false;' >刪除</a></td></tr>";
}
return $this->view;
}
//endallfunc所有函數結束
}
?>
require("Tree.php");
mysql_connect('localhost','root','');
mysql_select_db('test');
mysql_query('set names utf8');
$result = mysql_query("select * from t_class");
$data = array();
while ($row = mysql_fetch_array($result)){
$data[] = array('iClassID'=>$row['iClassID'],'iParentID'=>$row['iParentID'],'cClassName'=>$row['cClassName']);
}
$t = new Tree($data);
this->assign('class',$t->view());//表格樣式,主要是用來編輯和刪除分類
$this->assign('sclass',$t->select($id));//這里需要傳一個父id,當編輯的時候,以便標識此分類所在的父分類為選中狀態,如果為0 則顯示的是分部分類
$this->display();
<table>
{$class}//表格樣式
</table>
<select name='iParent'>
<option value='0'>---根分類---</option>
{$sclass}//下拉列表樣式
</select>
最終效果如圖:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

