信息private$cateArray=ar" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

一個無限分類類

系統 1789 0
這個是經過本人實踐確實可以使用的。
先說下表結構。一共三個字段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元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 宽甸| 巢湖市| 河西区| 屯昌县| 东源县| 肥东县| 南江县| 平利县| 江孜县| 田阳县| 临武县| 依安县| 乌鲁木齐县| 怀远县| 丹寨县| 宣化县| 宜川县| 义马市| 军事| 郎溪县| 米泉市| 海阳市| 天峻县| 太原市| 洛阳市| 亳州市| 嵊泗县| 安岳县| 永平县| 阿瓦提县| 长宁区| 平度市| 修武县| 横峰县| 九江县| 铜山县| 咸阳市| 若尔盖县| 城口县| 武穴市| 海林市|