自己前幾天寫的無(wú)限分類類
Jun 13, 2016 pm 12:34 PM
前一周寫的吧,使用中效果還不錯(cuò)。
?主要思想來(lái)自:http://www.phpobject.net/b...[url=http://www.phpobject.net/blog/read.php?49][/url]
? 這里就不多解釋原理了,直接發(fā)代碼。
? PS:這里代碼是不能直接使用的,必須結(jié)合我的一些其他庫(kù)類。應(yīng)該說(shuō)思想才是最重要的,這里主要提供一種分類的思路。
復(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????????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['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????????if(!empty($deep))$where.='?and?level????????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????????$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;?
????}?
????/**?
??????*?按照層次顯示分類?
??????*?@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);?
????}?
}?
?>?

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You may have encountered the problem of green lines appearing on the screen of your smartphone. Even if you have never seen it, you must have seen related pictures on the Internet. So, have you ever encountered a situation where the smart watch screen turns white? On April 2, CNMO learned from foreign media that a Reddit user shared a picture on the social platform, showing the screen of the Samsung Watch series smart watches turning white. The user wrote: "I was charging when I left, and when I came back, it was like this. I tried to restart, but the screen was still like this during the restart process." Samsung Watch smart watch screen turned white. The Reddit user did not specify the smart watch. Specific model. However, judging from the picture, it should be Samsung Watch5. Previously, another Reddit user also reported

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

Speaking of ASSASSIN, I believe players will definitely think of the master assassins in "Assassin's Creed". They are not only skilled, but also have the creed of "devoting themselves to the darkness and serving the light". The ASSASSIN series of flagship air-cooled radiators from the appliance brand DeepCool coincide with each other. Recently, the latest product of this series, ASSASSIN4S, has been launched. "Assassin in Suit, Advanced" brings a new air-cooling experience to advanced players. The appearance is full of details. The Assassin 4S radiator adopts a double tower structure + a single fan built-in design. The outside is covered with a cube-shaped fairing, which has a strong overall sense. It is available in white and black colors to meet different colors. Tie

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

With its compact size, the ITX platform has attracted many players who pursue the ultimate and unique beauty. With the improvement of manufacturing processes and technological advancements, both Intel's 14th generation Core and RTX40 series graphics cards can exert their strength on the ITX platform, and gamers also There are higher requirements for SFX power supply. Game enthusiast Huntkey has launched a new MX series power supply. In the ITX platform that meets high-performance requirements, the MX750P full-module power supply has a rated power of up to 750W and has passed 80PLUS platinum level certification. Below we bring the evaluation of this power supply. Huntkey MX750P full-module power supply adopts a simple and fashionable design concept. There are two black and white models for players to choose from. Both use matte surface treatment and have a good texture with silver gray and red fonts.
