信息private$cateArray=ar" />

黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

一個無限分類類

系統 2041 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條評論