自己前幾天寫的無限分類類
更新時間:2007年02月14日 00:00:00 作者:
前一周寫的吧,使用中效果還不錯。
主要思想來自:http://www.phpobject.net/b...[url=http://www.phpobject.net/blog/read.php?49][/url]
這里就不多解釋原理了,直接發(fā)代碼。
PS:這里代碼是不能直接使用的,必須結(jié)合我的一些其他庫類。應(yīng)該說思想才是最重要的,這里主要提供一種分類的思路。
<?
/**
--
-- 表的結(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é)點
* @param array $node 待增加子節(jié)點的屬性
* @param int $pid 父節(jié)點的ID
*/
function add($node,$pid){
//檢查是否已經(jīng)存在該節(jié)點
if($node_exist=$this->module->detail('where pid='.$pid.' and cname=\''.$node['cname'].'\'')){
//$this->error(__FUNCTION__.'():該節(jié)點'.$node['cname'].'已經(jīng)存在!');
//print_r($node_exist);
return $node_exist['cid'];
}
//獲取父節(jié)點信息
$pnode=$this->get_by_cid($pid);
//更新其他節(jié)點
$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é)點
$node['pid']=$pid;
$node['lft']=$pnode['rgt'];
$node['rgt']=$pnode['rgt']+1;
$node['level']=$pnode['level']+1;//層次加一
return $this->module->add($node);
}
/**
* 刪除節(jié)點
* @param $cid 待刪除的節(jié)點的ID
* @param $delete_childern 如果該節(jié)點存在子節(jié)點,是否強制刪除。設(shè)置未true,則當(dāng)存在子節(jié)點的時候,刪除失敗,返回false
*
*/
function delete($cid,$delete_childern=false)
{
//獲取節(jié)點信息
$node=$this->get_by_cid($cid);
if(($this->child_num($node)>0)&&(!$delete_childern))$this->error(__FUNCTION__.'():該節(jié)點存在子節(jié)點!');
//刪除該節(jié)點及其所有子節(jié)點
$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;
}
/**
* 更新一個節(jié)點
* @param array $set更新集
* @param int $cid 更新的節(jié)點的主鍵ID
*/
function update($set,$cid){
return $this->module->update($set,'where cid='.$cid);
}
/**
* 選取節(jié)點及其子節(jié)點
* @param int $cid節(jié)點的主鍵ID
* @param int $deep選取深度
*/
function select($cid,$deep=0)
{
//獲取節(jié)點信息
$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é)點路徑
* @param int $cid 節(jié)點的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é)點
* @param int $cid節(jié)點的主鍵ID
* @param int $deep選取深度
*/
function get_children($pid,$deep=0){
//獲取節(jié)點信息
$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é)點
* @param int $cid節(jié)點的主鍵ID
* @param int $deep選取深度
*/
function get_level_children($pid,$deep){
//獲取節(jié)點信息
$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é)點信息
* @param $cid 節(jié)點的主鍵ID
* @return array $node
*/
function get_by_cid($cid){
$node=$this->module->detail('where cid='.$cid);
if(!$node)$this->error(__FUNCTION__.'():獲取節(jié)點'.$cid.'失??!');
return $node;
}
/**
* 獲取子節(jié)點的數(shù)目
* @param array $node 節(jié)點信息
* @return num
*/
function child_num($node){
return ($node['rgt']-$node['lft']-1)/2;
}
/**
* 按照層次顯示分類
* @param int $cid節(jié)點的主鍵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);
}
}
?>
主要思想來自:http://www.phpobject.net/b...[url=http://www.phpobject.net/blog/read.php?49][/url]
這里就不多解釋原理了,直接發(fā)代碼。
PS:這里代碼是不能直接使用的,必須結(jié)合我的一些其他庫類。應(yīng)該說思想才是最重要的,這里主要提供一種分類的思路。
復(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é)點
* @param array $node 待增加子節(jié)點的屬性
* @param int $pid 父節(jié)點的ID
*/
function add($node,$pid){
//檢查是否已經(jīng)存在該節(jié)點
if($node_exist=$this->module->detail('where pid='.$pid.' and cname=\''.$node['cname'].'\'')){
//$this->error(__FUNCTION__.'():該節(jié)點'.$node['cname'].'已經(jīng)存在!');
//print_r($node_exist);
return $node_exist['cid'];
}
//獲取父節(jié)點信息
$pnode=$this->get_by_cid($pid);
//更新其他節(jié)點
$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é)點
$node['pid']=$pid;
$node['lft']=$pnode['rgt'];
$node['rgt']=$pnode['rgt']+1;
$node['level']=$pnode['level']+1;//層次加一
return $this->module->add($node);
}
/**
* 刪除節(jié)點
* @param $cid 待刪除的節(jié)點的ID
* @param $delete_childern 如果該節(jié)點存在子節(jié)點,是否強制刪除。設(shè)置未true,則當(dāng)存在子節(jié)點的時候,刪除失敗,返回false
*
*/
function delete($cid,$delete_childern=false)
{
//獲取節(jié)點信息
$node=$this->get_by_cid($cid);
if(($this->child_num($node)>0)&&(!$delete_childern))$this->error(__FUNCTION__.'():該節(jié)點存在子節(jié)點!');
//刪除該節(jié)點及其所有子節(jié)點
$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;
}
/**
* 更新一個節(jié)點
* @param array $set更新集
* @param int $cid 更新的節(jié)點的主鍵ID
*/
function update($set,$cid){
return $this->module->update($set,'where cid='.$cid);
}
/**
* 選取節(jié)點及其子節(jié)點
* @param int $cid節(jié)點的主鍵ID
* @param int $deep選取深度
*/
function select($cid,$deep=0)
{
//獲取節(jié)點信息
$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é)點路徑
* @param int $cid 節(jié)點的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é)點
* @param int $cid節(jié)點的主鍵ID
* @param int $deep選取深度
*/
function get_children($pid,$deep=0){
//獲取節(jié)點信息
$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é)點
* @param int $cid節(jié)點的主鍵ID
* @param int $deep選取深度
*/
function get_level_children($pid,$deep){
//獲取節(jié)點信息
$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é)點信息
* @param $cid 節(jié)點的主鍵ID
* @return array $node
*/
function get_by_cid($cid){
$node=$this->module->detail('where cid='.$cid);
if(!$node)$this->error(__FUNCTION__.'():獲取節(jié)點'.$cid.'失??!');
return $node;
}
/**
* 獲取子節(jié)點的數(shù)目
* @param array $node 節(jié)點信息
* @return num
*/
function child_num($node){
return ($node['rgt']-$node['lft']-1)/2;
}
/**
* 按照層次顯示分類
* @param int $cid節(jié)點的主鍵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);
}
}
?>
您可能感興趣的文章:
- 解析左右值無限分類的實現(xiàn)算法
- 解析thinkphp的左右值無限分類
- php無限分類且支持輸出樹狀圖的詳細介紹
- 利用php遞歸實現(xiàn)無限分類 格式化數(shù)組的詳解
- PHP無限分類(樹形類)的深入分析
- 基于php無限分類的深入理解
- 比較簡單實用的PHP無限分類源碼分享(思路不錯)
- PHP 無限分類三種方式 非函數(shù)的遞歸調(diào)用!
- PHP無限分類代碼,支持數(shù)組格式化、直接輸出菜單兩種方式
- 一個很簡單的無限分類樹實現(xiàn)代碼
- php遞歸實現(xiàn)無限分類生成下拉列表的函數(shù)
- php用數(shù)組返回?zé)o限分類的列表數(shù)據(jù)的代碼
- 刪除無限分類并同時刪除它下面的所有子分類的方法
- php 無限分類的樹類代碼
- asp.net 無限分類
- 幾篇關(guān)于無限分類算法的文章
- 帖幾個PHP的無限分類實現(xiàn)想法~
- PHP 循環(huán)刪除無限分類子節(jié)點的實現(xiàn)代碼
相關(guān)文章
php運行出現(xiàn)Call to undefined function curl_init()的解決方法
curl_init -- 初始化一個CURL會話,如果提示Call to undefined function curl_init那么需要如下操作即可。2010-11-11php使用imagecopymerge()函數(shù)創(chuàng)建半透明水印
這篇文章主要介紹了php使用imagecopymerge()函數(shù)創(chuàng)建半透明水印,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01php使用mysqli和pdo擴展,測試對比連接mysql數(shù)據(jù)庫的效率完整示例
這篇文章主要介紹了php使用mysqli和pdo擴展,測試對比連接mysql數(shù)據(jù)庫的效率,結(jié)合完整實例形式對比分析了php分別使用mysqli和pdo擴展連接mysql數(shù)據(jù)庫的執(zhí)行時間,需要的朋友可以參考下2019-05-05