信息private$cateArray=ar" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

一個(gè)無限分類類

系統(tǒng) 1666 0
這個(gè)是經(jīng)過本人實(shí)踐確實(shí)可以使用的。
先說下表結(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>

  

最終效果如圖:

一個(gè)無限分類類





一個(gè)無限分類類


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久天天躁狠狠躁夜夜爽蜜月 | 操熟美女又肥又嫩的骚屁股 | 色偷偷女人的天堂a在线 | 一级影院 | 97福利影院 | 亚洲国产人成中文幕一级二级 | 亚洲高清一区二区三区久久 | 国产免费爱在线观看视频 | 精品国偷自产在线 | 四虎免费观看 | 大色佬视频在线观看 | 99麻豆视频| 久久精品中文字幕一区 | 久久亚洲成人 | 亚洲线精品久久一区二区三区 | 高清国产一区二区 | 中文字幕国产在线观看 | 国产成人精品一区二三区2022 | 久久精品国产一区二区三区日韩 | 国产综合日韩伦理 | 最近中文日本字幕免费完整 | 亚洲无线码一区在线观看 | 国产欧美在线观看 | 中文字幕一区久久久久 | 亚洲色视频 | 高清成人| 美女被羞羞在线观看 | 性做久久久久久网站 | 国产日产欧美一区二区三区 | 外国成人网在线观看免费视频 | 午夜激情网站 | 国产精品视频免费视频 | 无人码一区二区三区视频 | 久热精品香蕉在线视频 | 亚洲精品久久 | 99影视在线视频免费观看 | 亚洲综合精品一区二区三区中文 | 日韩欧美视频一区二区在线观看 | 亚洲伊人久久综合 | 卡通动漫精选国产欧美 | 久久亚洲精品中文字幕 |