這個(gè)是經(jīng)過本人實(shí)踐確實(shí)可以使用的。
先說下表結(jié)構(gòu)。一共三個(gè)字段iClassID,iParentID,cClassName;
一個(gè)是分類的id,一個(gè)是父id,一個(gè)是分類的名字,
下面是代碼:
最終效果如圖:
先說下表結(jié)構(gòu)。一共三個(gè)字段iClassID,iParentID,cClassName;
一個(gè)是分類的id,一個(gè)是父id,一個(gè)是分類的名字,
下面是代碼:
<?php class Tree { /* *在這里需要注意的是,類需要傳入一個(gè)數(shù)組。即分類的全部數(shù)據(jù),是一個(gè)二維的數(shù)組 * */ //分層 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(); } //設(shè)置樹的節(jié)點(diǎn) function setNode($id, $parent, $value) { $parent = $parent?$parent:0; $this->data[$id] = $value; $this->cateArray[$id] = $parent; } /* * 遞歸實(shí)現(xiàn) * 得到id下的子樹結(jié)構(gòu)數(shù)組(用多維數(shù)組格式來表示樹) * id Internet 節(jié)點(diǎn)id (0表示的是根節(jié)點(diǎn),是虛擬的,即沒有與它對應(yīng)的實(shí)際信息) * return array 子樹節(jié)點(diǎn)數(shù)組 */ function getChildsTree($id=0) { $childs=array(); foreach ($this->cateArray as $child=>$parent) { if ($parent==$id) { $childs[$child]=$this->getChildsTree($child); } } return $childs; } /* * 遞歸實(shí)現(xiàn) * 得到id節(jié)點(diǎn)的所有后代節(jié)點(diǎn) * $id * return array 索引=>id,...一維數(shù)組(順序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節(jié)點(diǎn)的孩子節(jié)點(diǎn) * $id * return array 索引=>id,...一維數(shù)組 */ function getChild($id) { $childs=array(); foreach ($this->cateArray as $child=>$parent) { if ($parent==$id) { $childs[]=$child; } } return $childs; } /* * 遞歸實(shí)現(xiàn) * 反線獲得節(jié)點(diǎn)id的父節(jié)點(diǎn) * $id interger 不可以為0 * return array 其父節(jié)點(diǎn)數(shù)組 */ function getNodeLever($id) { $parents=array(); if (array_key_exists($this->cateArray[$id],$this->cateArray))//它的父節(jié)點(diǎn),在節(jié)點(diǎn)樹中 { $parents[]=$this->cateArray[$id]; $parents=array_merge($parents,$this->getNodeLever($this->cateArray[$id])); } return $parents; } /* * 根據(jù)所在層數(shù)得到n-1個(gè)前導(dǎo)格式化符號 * $id Internet 不可以取0 * $preStr str 填充的格式化符號 * return str 多個(gè)格式化符號 */ function getLayer($id,$preStr='|-') { return str_repeat($preStr,count($this->getNodeLever($id))); } //得到id節(jié)點(diǎn)的信息 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所有函數(shù)結(jié)束 } ?>
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));//這里需要傳一個(gè)父id,當(dāng)編輯的時(shí)候,以便標(biāo)識此分類所在的父分類為選中狀態(tài),如果為0 則顯示的是分部分類 $this->display();
<table> {$class}//表格樣式 </table> <select name='iParent'> <option value='0'>---根分類---</option> {$sclass}//下拉列表樣式 </select>
最終效果如圖:


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
