自己前幾天寫(xiě)的無(wú)限分類(lèi)類(lèi)
更新時(shí)間:2007年02月14日 00:00:00 作者:
前一周寫(xiě)的吧,使用中效果還不錯(cuò)。
主要思想來(lái)自:http://www.phpobject.net/b...[url=http://www.phpobject.net/blog/read.php?49][/url]
這里就不多解釋原理了,直接發(fā)代碼。
PS:這里代碼是不能直接使用的,必須結(jié)合我的一些其他庫(kù)類(lèi)。應(yīng)該說(shuō)思想才是最重要的,這里主要提供一種分類(lèi)的思路。
<?
/**
--
-- 表的結(jié)構(gòu) `daxue8_category`
--
CREATE TABLE `daxue8_category` (
`cid` smallint(6) NOT NULL auto_increment,
`pid` smallint(6) NOT NULL default '0',
`level` smallint(6) NOT NULL default '0',
`cname` char(64) NOT NULL default '',
`lft` smallint(6) NOT NULL default '0',
`rgt` smallint(6) NOT NULL default '0',
`uid` mediumint(8) NOT NULL default '0',
`username` char(32) NOT NULL default '',
`ctime` int(10) NOT NULL default '0',
`cstate` tinyint(1) NOT NULL default '0',
`gnum` mediumint(8) NOT NULL default '0',
`orderstyle` smallint(3) NOT NULL default '0',
PRIMARY KEY (`cid`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
--
-- 導(dǎo)出表中的數(shù)據(jù) `daxue8_category`
--
INSERT INTO `daxue8_category` VALUES (1, 0, 1, 'root', 1, 2, 0, '管理員', 1163608814, 1, 0, 0);
*/
class category
{
var $module;
var $tbname;
function category()
{
$this->tbname=TB_PREX.'_category';
$this->module=new module($this->tbname);
}
/**
* 增加子節(jié)點(diǎn)
* @param array $node 待增加子節(jié)點(diǎn)的屬性
* @param int $pid 父節(jié)點(diǎn)的ID
*/
function add($node,$pid){
//檢查是否已經(jīng)存在該節(jié)點(diǎn)
if($node_exist=$this->module->detail('where pid='.$pid.' and cname=\''.$node['cname'].'\'')){
//$this->error(__FUNCTION__.'():該節(jié)點(diǎn)'.$node['cname'].'已經(jīng)存在!');
//print_r($node_exist);
return $node_exist['cid'];
}
//獲取父節(jié)點(diǎn)信息
$pnode=$this->get_by_cid($pid);
//更新其他節(jié)點(diǎn)
$this->module->query('update `'.$this->tbname.'` set lft=lft+2 where lft>'.$pnode['rgt']);
$this->module->query('update `'.$this->tbname.'` set rgt=rgt+2 where rgt>='.$pnode['rgt']);
//插入新節(jié)點(diǎn)
$node['pid']=$pid;
$node['lft']=$pnode['rgt'];
$node['rgt']=$pnode['rgt']+1;
$node['level']=$pnode['level']+1;//層次加一
return $this->module->add($node);
}
/**
* 刪除節(jié)點(diǎn)
* @param $cid 待刪除的節(jié)點(diǎn)的ID
* @param $delete_childern 如果該節(jié)點(diǎn)存在子節(jié)點(diǎn),是否強(qiáng)制刪除。設(shè)置未true,則當(dāng)存在子節(jié)點(diǎn)的時(shí)候,刪除失敗,返回false
*
*/
function delete($cid,$delete_childern=false)
{
//獲取節(jié)點(diǎn)信息
$node=$this->get_by_cid($cid);
if(($this->child_num($node)>0)&&(!$delete_childern))$this->error(__FUNCTION__.'():該節(jié)點(diǎn)存在子節(jié)點(diǎn)!');
//刪除該節(jié)點(diǎn)及其所有子節(jié)點(diǎn)
$this->module->delete('where lft between '.$node['lft'].' and '.$node['rgt']);
//修改相應(yīng)的左右鍵值
$plus=$node['rgt']-$node['lft']+1;
$this->module->query('update `'.$this->tbname.'` set lft=lft-'.$plus.' where lft>'.$node['rgt']);
$this->module->query('update `'.$this->tbname.'` set rgt=rgt-'.$plus.' where rgt>'.$node['rgt']);
return true;
}
/**
* 更新一個(gè)節(jié)點(diǎn)
* @param array $set更新集
* @param int $cid 更新的節(jié)點(diǎn)的主鍵ID
*/
function update($set,$cid){
return $this->module->update($set,'where cid='.$cid);
}
/**
* 選取節(jié)點(diǎn)及其子節(jié)點(diǎn)
* @param int $cid節(jié)點(diǎn)的主鍵ID
* @param int $deep選取深度
*/
function select($cid,$deep=0)
{
//獲取節(jié)點(diǎn)信息
$node=$this->get_by_cid($cid);
$where='where lft between '.$node['lft'].' and '.$node['rgt'];
if(!empty($deep))$where.=' and level<'.$node['level']+$deep;
if($deep==1){
$where.=' order by orderstyle desc';
}else{
$where.=' order by lft asc';
}
return $this->module->select($where);
}
/**
* 獲取父節(jié)點(diǎn)路徑
* @param int $cid 節(jié)點(diǎn)的ID
*/
function get_parent($cid)
{
$node=$this->get_by_cid($cid);
return $this->module->select('where lft<='.$node['lft'].' and rgt>='.$node['rgt'].' order by lft asc');
}
/**
* 選取子節(jié)點(diǎn)
* @param int $cid節(jié)點(diǎn)的主鍵ID
* @param int $deep選取深度
*/
function get_children($pid,$deep=0){
//獲取節(jié)點(diǎn)信息
$pnode=$this->get_by_cid($pid);
$where='where lft>'.$pnode['lft'].' and rgt<'.$pnode['rgt'];
if(!empty($deep))$where.=' and level<='.($pnode['level']+$deep);
if($deep==1){
$where.=' order by orderstyle desc';
}else{
$where.=' order by lft asc';
}
return $this->module->select($where);
}
/**
* 獲取第deep層子節(jié)點(diǎn)
* @param int $cid節(jié)點(diǎn)的主鍵ID
* @param int $deep選取深度
*/
function get_level_children($pid,$deep){
//獲取節(jié)點(diǎn)信息
$pnode=$this->get_by_cid($pid);
$where='where lft>'.$pnode['lft'].' and rgt<'.$pnode['rgt'];
$where.=' and level='.($pnode['level']+$deep);
$where.=' order by orderstyle desc';
return $this->module->select($where);
}
/**
* 獲取節(jié)點(diǎn)信息
* @param $cid 節(jié)點(diǎn)的主鍵ID
* @return array $node
*/
function get_by_cid($cid){
$node=$this->module->detail('where cid='.$cid);
if(!$node)$this->error(__FUNCTION__.'():獲取節(jié)點(diǎn)'.$cid.'失?。?);
return $node;
}
/**
* 獲取子節(jié)點(diǎn)的數(shù)目
* @param array $node 節(jié)點(diǎn)信息
* @return num
*/
function child_num($node){
return ($node['rgt']-$node['lft']-1)/2;
}
/**
* 按照層次顯示分類(lèi)
* @param int $cid節(jié)點(diǎn)的主鍵ID
* @output
*/
function display($cid)
{
$nodes=$this->select($cid);
foreach($nodes as $node){
echo str_repeat(' ',$node['level']-1).$node['cname']."\n";
}
}
/*-------private-----------------------------------*/
function error($msg){
die('ERROR : file '.__FILE__.' function '.$msg);
}
}
?>
主要思想來(lái)自:http://www.phpobject.net/b...[url=http://www.phpobject.net/blog/read.php?49][/url]
這里就不多解釋原理了,直接發(fā)代碼。
PS:這里代碼是不能直接使用的,必須結(jié)合我的一些其他庫(kù)類(lèi)。應(yīng)該說(shuō)思想才是最重要的,這里主要提供一種分類(lèi)的思路。
復(fù)制代碼 代碼如下:
<?
/**
--
-- 表的結(jié)構(gòu) `daxue8_category`
--
CREATE TABLE `daxue8_category` (
`cid` smallint(6) NOT NULL auto_increment,
`pid` smallint(6) NOT NULL default '0',
`level` smallint(6) NOT NULL default '0',
`cname` char(64) NOT NULL default '',
`lft` smallint(6) NOT NULL default '0',
`rgt` smallint(6) NOT NULL default '0',
`uid` mediumint(8) NOT NULL default '0',
`username` char(32) NOT NULL default '',
`ctime` int(10) NOT NULL default '0',
`cstate` tinyint(1) NOT NULL default '0',
`gnum` mediumint(8) NOT NULL default '0',
`orderstyle` smallint(3) NOT NULL default '0',
PRIMARY KEY (`cid`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
--
-- 導(dǎo)出表中的數(shù)據(jù) `daxue8_category`
--
INSERT INTO `daxue8_category` VALUES (1, 0, 1, 'root', 1, 2, 0, '管理員', 1163608814, 1, 0, 0);
*/
class category
{
var $module;
var $tbname;
function category()
{
$this->tbname=TB_PREX.'_category';
$this->module=new module($this->tbname);
}
/**
* 增加子節(jié)點(diǎn)
* @param array $node 待增加子節(jié)點(diǎn)的屬性
* @param int $pid 父節(jié)點(diǎn)的ID
*/
function add($node,$pid){
//檢查是否已經(jīng)存在該節(jié)點(diǎn)
if($node_exist=$this->module->detail('where pid='.$pid.' and cname=\''.$node['cname'].'\'')){
//$this->error(__FUNCTION__.'():該節(jié)點(diǎn)'.$node['cname'].'已經(jīng)存在!');
//print_r($node_exist);
return $node_exist['cid'];
}
//獲取父節(jié)點(diǎn)信息
$pnode=$this->get_by_cid($pid);
//更新其他節(jié)點(diǎn)
$this->module->query('update `'.$this->tbname.'` set lft=lft+2 where lft>'.$pnode['rgt']);
$this->module->query('update `'.$this->tbname.'` set rgt=rgt+2 where rgt>='.$pnode['rgt']);
//插入新節(jié)點(diǎn)
$node['pid']=$pid;
$node['lft']=$pnode['rgt'];
$node['rgt']=$pnode['rgt']+1;
$node['level']=$pnode['level']+1;//層次加一
return $this->module->add($node);
}
/**
* 刪除節(jié)點(diǎn)
* @param $cid 待刪除的節(jié)點(diǎn)的ID
* @param $delete_childern 如果該節(jié)點(diǎn)存在子節(jié)點(diǎn),是否強(qiáng)制刪除。設(shè)置未true,則當(dāng)存在子節(jié)點(diǎn)的時(shí)候,刪除失敗,返回false
*
*/
function delete($cid,$delete_childern=false)
{
//獲取節(jié)點(diǎn)信息
$node=$this->get_by_cid($cid);
if(($this->child_num($node)>0)&&(!$delete_childern))$this->error(__FUNCTION__.'():該節(jié)點(diǎn)存在子節(jié)點(diǎn)!');
//刪除該節(jié)點(diǎn)及其所有子節(jié)點(diǎn)
$this->module->delete('where lft between '.$node['lft'].' and '.$node['rgt']);
//修改相應(yīng)的左右鍵值
$plus=$node['rgt']-$node['lft']+1;
$this->module->query('update `'.$this->tbname.'` set lft=lft-'.$plus.' where lft>'.$node['rgt']);
$this->module->query('update `'.$this->tbname.'` set rgt=rgt-'.$plus.' where rgt>'.$node['rgt']);
return true;
}
/**
* 更新一個(gè)節(jié)點(diǎn)
* @param array $set更新集
* @param int $cid 更新的節(jié)點(diǎn)的主鍵ID
*/
function update($set,$cid){
return $this->module->update($set,'where cid='.$cid);
}
/**
* 選取節(jié)點(diǎn)及其子節(jié)點(diǎn)
* @param int $cid節(jié)點(diǎn)的主鍵ID
* @param int $deep選取深度
*/
function select($cid,$deep=0)
{
//獲取節(jié)點(diǎn)信息
$node=$this->get_by_cid($cid);
$where='where lft between '.$node['lft'].' and '.$node['rgt'];
if(!empty($deep))$where.=' and level<'.$node['level']+$deep;
if($deep==1){
$where.=' order by orderstyle desc';
}else{
$where.=' order by lft asc';
}
return $this->module->select($where);
}
/**
* 獲取父節(jié)點(diǎn)路徑
* @param int $cid 節(jié)點(diǎn)的ID
*/
function get_parent($cid)
{
$node=$this->get_by_cid($cid);
return $this->module->select('where lft<='.$node['lft'].' and rgt>='.$node['rgt'].' order by lft asc');
}
/**
* 選取子節(jié)點(diǎn)
* @param int $cid節(jié)點(diǎn)的主鍵ID
* @param int $deep選取深度
*/
function get_children($pid,$deep=0){
//獲取節(jié)點(diǎn)信息
$pnode=$this->get_by_cid($pid);
$where='where lft>'.$pnode['lft'].' and rgt<'.$pnode['rgt'];
if(!empty($deep))$where.=' and level<='.($pnode['level']+$deep);
if($deep==1){
$where.=' order by orderstyle desc';
}else{
$where.=' order by lft asc';
}
return $this->module->select($where);
}
/**
* 獲取第deep層子節(jié)點(diǎn)
* @param int $cid節(jié)點(diǎn)的主鍵ID
* @param int $deep選取深度
*/
function get_level_children($pid,$deep){
//獲取節(jié)點(diǎn)信息
$pnode=$this->get_by_cid($pid);
$where='where lft>'.$pnode['lft'].' and rgt<'.$pnode['rgt'];
$where.=' and level='.($pnode['level']+$deep);
$where.=' order by orderstyle desc';
return $this->module->select($where);
}
/**
* 獲取節(jié)點(diǎn)信息
* @param $cid 節(jié)點(diǎn)的主鍵ID
* @return array $node
*/
function get_by_cid($cid){
$node=$this->module->detail('where cid='.$cid);
if(!$node)$this->error(__FUNCTION__.'():獲取節(jié)點(diǎn)'.$cid.'失?。?);
return $node;
}
/**
* 獲取子節(jié)點(diǎn)的數(shù)目
* @param array $node 節(jié)點(diǎn)信息
* @return num
*/
function child_num($node){
return ($node['rgt']-$node['lft']-1)/2;
}
/**
* 按照層次顯示分類(lèi)
* @param int $cid節(jié)點(diǎn)的主鍵ID
* @output
*/
function display($cid)
{
$nodes=$this->select($cid);
foreach($nodes as $node){
echo str_repeat(' ',$node['level']-1).$node['cname']."\n";
}
}
/*-------private-----------------------------------*/
function error($msg){
die('ERROR : file '.__FILE__.' function '.$msg);
}
}
?>
您可能感興趣的文章:
- 解析左右值無(wú)限分類(lèi)的實(shí)現(xiàn)算法
- 解析thinkphp的左右值無(wú)限分類(lèi)
- php無(wú)限分類(lèi)且支持輸出樹(shù)狀圖的詳細(xì)介紹
- 利用php遞歸實(shí)現(xiàn)無(wú)限分類(lèi) 格式化數(shù)組的詳解
- PHP無(wú)限分類(lèi)(樹(shù)形類(lèi))的深入分析
- 基于php無(wú)限分類(lèi)的深入理解
- 比較簡(jiǎn)單實(shí)用的PHP無(wú)限分類(lèi)源碼分享(思路不錯(cuò))
- PHP 無(wú)限分類(lèi)三種方式 非函數(shù)的遞歸調(diào)用!
- PHP無(wú)限分類(lèi)代碼,支持?jǐn)?shù)組格式化、直接輸出菜單兩種方式
- 一個(gè)很簡(jiǎn)單的無(wú)限分類(lèi)樹(shù)實(shí)現(xiàn)代碼
- php遞歸實(shí)現(xiàn)無(wú)限分類(lèi)生成下拉列表的函數(shù)
- php用數(shù)組返回?zé)o限分類(lèi)的列表數(shù)據(jù)的代碼
- 刪除無(wú)限分類(lèi)并同時(shí)刪除它下面的所有子分類(lèi)的方法
- php 無(wú)限分類(lèi)的樹(shù)類(lèi)代碼
- asp.net 無(wú)限分類(lèi)
- 幾篇關(guān)于無(wú)限分類(lèi)算法的文章
- 帖幾個(gè)PHP的無(wú)限分類(lèi)實(shí)現(xiàn)想法~
- PHP 循環(huán)刪除無(wú)限分類(lèi)子節(jié)點(diǎn)的實(shí)現(xiàn)代碼
相關(guān)文章
php運(yùn)行出現(xiàn)Call to undefined function curl_init()的解決方法
curl_init -- 初始化一個(gè)CURL會(huì)話,如果提示Call to undefined function curl_init那么需要如下操作即可。2010-11-11PHP中最低級(jí)別的錯(cuò)誤類(lèi)型總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于PHP中最低級(jí)別的錯(cuò)誤類(lèi)型總結(jié)內(nèi)容,對(duì)此有興趣的朋友們可以跟著學(xué)習(xí)下。2022-01-01php使用imagecopymerge()函數(shù)創(chuàng)建半透明水印
這篇文章主要介紹了php使用imagecopymerge()函數(shù)創(chuàng)建半透明水印,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01php使用mysqli和pdo擴(kuò)展,測(cè)試對(duì)比連接mysql數(shù)據(jù)庫(kù)的效率完整示例
這篇文章主要介紹了php使用mysqli和pdo擴(kuò)展,測(cè)試對(duì)比連接mysql數(shù)據(jù)庫(kù)的效率,結(jié)合完整實(shí)例形式對(duì)比分析了php分別使用mysqli和pdo擴(kuò)展連接mysql數(shù)據(jù)庫(kù)的執(zhí)行時(shí)間,需要的朋友可以參考下2019-05-05php 注冊(cè)時(shí)輸入信息驗(yàn)證器的實(shí)現(xiàn)詳解
本篇文章是對(duì)php中注冊(cè)時(shí)輸入信息驗(yàn)證器的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07利用PHP如何實(shí)現(xiàn)Socket服務(wù)器
想要構(gòu)建聊天應(yīng)用,或者甚至是游戲嗎?那么,socket服務(wù)器將成為你邁出的第一步。一旦你了解了創(chuàng)建服務(wù)器的基本功能,那么后續(xù)的優(yōu)化步驟就會(huì)變得同樣簡(jiǎn)單,需要的朋友可以參考下2015-09-09