Home php教程 php手册 自己前几天写的无限分类类

自己前几天写的无限分类类

Jun 13, 2016 pm 12:34 PM
http main use point Thought Effect unlimited Own

前一周写的吧,使用中效果还不错。

 主要思想来自:http://www.phpobject.net/b...[url=http://www.phpobject.net/blog/read.php?49][/url]

  这里就不多解释原理了,直接发代码。

  PS:这里代码是不能直接使用的,必须结合我的一些其他库类。应该说思想才是最重要的,这里主要提供一种分类的思路。

复制代码 代码如下:


 
/** 
--  
-- 表的结构 `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 ; 

--  
-- 导出表中的数据 `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); 
    } 

    /** 
      * 增加子节点 
      * @param array $node 待增加子节点的属性 
      * @param int $pid 父节点的ID 
    */ 
    function add($node,$pid){ 
        //检查是否已经存在该节点 
        if($node_exist=$this->module->detail('where pid='.$pid.' and cname=\''.$node['cname'].'\'')){ 
            //$this->error(__FUNCTION__.'():该节点'.$node['cname'].'已经存在!'); 
            //print_r($node_exist); 
            return $node_exist['cid']; 
        } 
        //获取父节点信息 
        $pnode=$this->get_by_cid($pid); 
        //更新其他节点 
        $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']); 
        //插入新节点 
        $node['pid']=$pid; 
        $node['lft']=$pnode['rgt']; 
        $node['rgt']=$pnode['rgt']+1; 
        $node['level']=$pnode['level']+1;//层次加一 
        return $this->module->add($node); 
    } 

    /** 
      * 删除节点 
      * @param $cid 待删除的节点的ID 
      * @param $delete_childern 如果该节点存在子节点,是否强制删除。设置未true,则当存在子节点的时候,删除失败,返回false 
      * 
    */ 
    function delete($cid,$delete_childern=false) 
    { 
        //获取节点信息 
        $node=$this->get_by_cid($cid); 
        if(($this->child_num($node)>0)&&(!$delete_childern))$this->error(__FUNCTION__.'():该节点存在子节点!'); 
        //删除该节点及其所有子节点 
        $this->module->delete('where lft between '.$node['lft'].' and '.$node['rgt']); 
        //修改相应的左右键值 
        $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; 
    } 

    /** 
      * 更新一个节点 
      * @param array $set更新集 
      * @param int $cid 更新的节点的主键ID 
    */ 
    function update($set,$cid){ 
        return $this->module->update($set,'where cid='.$cid); 
    } 

    /** 
      * 选取节点及其子节点 
      * @param int $cid节点的主键ID 
      * @param int $deep选取深度 
    */ 
    function select($cid,$deep=0) 
    { 
        //获取节点信息 
        $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); 
    } 

    /** 
      * 获取父节点路径 
      * @param int $cid 节点的ID  
    */ 
    function get_parent($cid) 
    { 
        $node=$this->get_by_cid($cid); 
        return $this->module->select('where lft='.$node['rgt'].' order by lft asc'); 
    } 
    /** 
      * 选取子节点 
      * @param int $cid节点的主键ID 
      * @param int $deep选取深度 
    */ 
    function get_children($pid,$deep=0){ 
        //获取节点信息 
        $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层子节点 
      * @param int $cid节点的主键ID 
      * @param int $deep选取深度 
    */ 
    function get_level_children($pid,$deep){ 
        //获取节点信息 
        $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); 
    } 

    /** 
      * 获取节点信息 
      * @param $cid 节点的主键ID 
      * @return array $node 
    */ 
    function get_by_cid($cid){ 
        $node=$this->module->detail('where cid='.$cid); 
        if(!$node)$this->error(__FUNCTION__.'():获取节点'.$cid.'失败!'); 
        return $node; 
    } 
    /** 
      * 获取子节点的数目 
      * @param array $node 节点信息 
      * @return num 
    */ 
    function child_num($node){ 
        return ($node['rgt']-$node['lft']-1)/2; 
    } 
    /** 
      * 按照层次显示分类 
      * @param int $cid节点的主键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); 
    } 

?> 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Users encounter rare glitches: Samsung Watch smartwatches suddenly experience white screen issues Users encounter rare glitches: Samsung Watch smartwatches suddenly experience white screen issues Apr 03, 2024 am 08:13 AM

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

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

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

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

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

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

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.

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

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

Kyushu Fengshen Assassin 4S Radiator Review Air-cooled 'Assassin Master' Style Kyushu Fengshen Assassin 4S Radiator Review Air-cooled 'Assassin Master' Style Mar 28, 2024 am 11:11 AM

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

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

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

Exquisite light and shadow art in spring, Haqu H2 is the cost-effective choice Exquisite light and shadow art in spring, Haqu H2 is the cost-effective choice Apr 17, 2024 pm 05:07 PM

With the arrival of spring, everything revives and everything is full of vitality and vitality. In this beautiful season, how to add a touch of color to your home life? Haqu H2 projector, with its exquisite design and super cost-effectiveness, has become an indispensable beauty in this spring. This H2 projector is compact yet stylish. Whether placed on the TV cabinet in the living room or next to the bedside table in the bedroom, it can become a beautiful landscape. Its body is made of milky white matte texture. This design not only makes the projector look more advanced, but also increases the comfort of the touch. The beige leather-like material adds a touch of warmth and elegance to the overall appearance. This combination of colors and materials not only conforms to the aesthetic trend of modern homes, but also can be integrated into

See all articles