分享

【Mysql左右值】左右值法实现Mysql无限级分类

 流浪港湾 2015-03-04

数据表结构和数据

表结构

  1. CREATE TABLE `classify` (  
  2. `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,  
  3. `name` VARCHAR(20) NOT NULL COLLATE 'utf8_general_ci',  
  4. `lft` INT(11) NOT NULL,  
  5. `rgt` INT(11) NOT NULL,  
  6. `parentId` INT(11) NOT NULL,  
  7. PRIMARY KEY (`id`),  
  8. INDEX `scope` (`lft`, `rgt`)  
  9. )  
  10. COLLATE='utf8_bin'  
  11. ENGINE=InnoDB  
  12. AUTO_INCREMENT=13;  

测试数据

  1. INSERT INTO `classify` VALUES (1, '郑州', 1, 16, -1);  
  2. INSERT INTO `classify` VALUES (19, '荥阳', 10, 15, 1);  
  3. INSERT INTO `classify` VALUES (20, '荥阳东', 13, 14, 19);  
  4. INSERT INTO `classify` VALUES (21, '荥阳西', 11, 12, 19);  
  5. INSERT INTO `classify` VALUES (22, '开封', 2, 9, 1);  
  6. INSERT INTO `classify` VALUES (23, '开封东', 7, 8, 22);  
  7. INSERT INTO `classify` VALUES (24, '开封西', 5, 6, 22);  
  8. INSERT INTO `classify` VALUES (25, '开封府', 3, 4, 22);  

文件结构

  1. /——  
  2.   |--index.php 入口文件  
  3.   |--conf.php  配置文件  
  4.   /db  
  5.   |--DB.class.php 数据库操作类  
  6.   /lib  
  7.   |--ClassifyTree.class.php 左右树操作文件【core】  

index.php 代码

  1. <?php  
  2. /** 
  3.  * action 
  4.  * showAll      查看整个分类树 
  5.  * showOne      查看某个分类的子类 
  6.  * showPath     查看某个子类到根分类的路径 
  7.  * showAdd      显示增加分类的界面 
  8.  * add          增加子分类 
  9.  * delete       删除分类 
  10.  * showModify   显示修改分类信息的界面 
  11.  * modify       修改分类信息 
  12.  * 
  13.  * 假设已经有个  根分类  郑州 
  14.  */  
  15. (isset($_GET['action']) && $action = $_GET['action'] ) || $action = 'showAll';  
  16. header('Content-type:text/html;charset=utf-8');  
  17.   
  18. //实例化分类操作类  
  19. include_once './db/DB.class.php';  
  20. $conf = include_once './conf.php';  
  21. include_once './lib/ClassifyTree.class.php';  
  22. $tree = new ClassifyTree($conf);  
  23.   
  24. /** 
  25.  * [显示增加分类的界面] 
  26.  * @return [boolean] [插入成功,返回true;否则,返回false] 
  27.  */  
  28. function add(){  
  29.     global $tree;  
  30.     $parentId = $_GET['parent'];  
  31.     $name     = $_GET['name'];  
  32.     $result   = $tree->insertNew($parentId, $name);  
  33.     return $result;  
  34. }  
  35.   
  36. /** 
  37.  * [显示增加分类页面] 
  38.  */  
  39. function showAdd(){  
  40.     $parentId = $_GET['parent'];  
  41.     echo '<form action="." method="get">';  
  42.     echo '<input type="hidden" name="action" value="add">';  
  43.     echo '<br/>名称:<input type="text" name="name" style="width:200px;">';  
  44.     echo '<input type="hidden" name="parent" value="'.$parentId.'">';  
  45.     echo '<br/><input type="submit" style="width:130px;height:25px;"></form>';  
  46. }  
  47.   
  48. /** 
  49.  * [显示一个子分类下的所有分类] 
  50.  */  
  51. function showOne(){  
  52.     global $tree;  
  53.     $id = $_GET['id'];  
  54.     $classifyInfo = $tree->getOne($id);  
  55.     //递归显示分类信息  
  56.     displayClassify($classifyInfo);  
  57. }  
  58.   
  59. /** 
  60.  * [显示某个分类到根分类的路径] 
  61.  */  
  62. function showPath(){  
  63.     global $tree;  
  64.     $id = $_GET['id'];  
  65.     $pathArr = $tree->getPath($id);  
  66.     displayPath($pathArr);  
  67. }  
  68.   
  69. function getPath($id){  
  70.     global $tree;  
  71.     $pathArr = $tree->getPath($id);  
  72.     displayPath($pathArr);  
  73. }  
  74.   
  75. /** 
  76.  * [显示所有的分类信息] 
  77.  * @return [type] [description] 
  78.  */  
  79. function showAll(){  
  80.     global $tree;  
  81.     $classifyArr = $tree->getAll();  
  82.     displayClassify($classifyArr);  
  83. }  
  84.   
  85. /** 
  86.  * [删除一个分类] 
  87.  * @return [boolean] [删除成功,则返回true;否则,则返回false] 
  88.  */  
  89. function delete(){  
  90.     global $tree;  
  91.     $id = $_GET['id'];  
  92.     $result = $tree->delete($id);  
  93.     return $result;  
  94. }  
  95.   
  96. /** 
  97.  * [显示修改id的界面] 
  98.  * @return [bolean] [如果查询失败,则返回false] 
  99.  */  
  100. function showModify(){  
  101.     global $tree;  
  102.     $id = $_GET['id'];  
  103.     $info = $tree->searchById($id);  
  104.     if(false === $info){  
  105.         return false;  
  106.     }  
  107.     echo '<form action="." method="get">';  
  108.     echo '<input type="hidden" name="action" value="modify">';  
  109.     echo '名称:<input type="text" name="name" value="'.$info['name'].'" style="width:200px;height:25px;">';  
  110.     echo '<input type="hidden" name="id" value="'.$info['id'].'">';  
  111.     echo '<br/><input type="submit" style="width:120px;height:25px;">';  
  112.     echo '</form>';  
  113. }  
  114.   
  115. /** 
  116.  * [修改分类信息] 
  117.  * @return [boolean] [修改成功,则返回true;否则,返回false] 
  118.  */  
  119. function modify(){  
  120.     global $tree;  
  121.     $name = $_GET['name'];  
  122.     $id   = $_GET['id'];  
  123.     $result = $tree->modify($name, $id);  
  124.     return $result;  
  125. }  
  126. /** 
  127.  * [输出路径数组] 
  128.  * @param  [array] $path [路径数组] 
  129.  */  
  130. function displayPath($path){  
  131.     foreach($path as $oneStep){  
  132.         echo '<a href="?action=showPath&id='.$oneStep['id'].'">'.$oneStep['name'].'</a> >> ';  
  133.     }  
  134. }  
  135.   
  136. /** 
  137.  * [递归显示分类信息] 
  138.  * @param  [array] $classify [分类信息] 
  139.  * @param  [int]   $interval [缩进长度] 
  140.  */  
  141. function displayClassify($classify, $interval=0){  
  142.     foreach($classify as $key=>$val){  
  143.         for($i=0;$i<$interval;$i++){  
  144.             echo '    ';  
  145.         }  
  146.         echo $key;  
  147.         if(is_array($val)){  
  148.             echo ' => array(<br/>';  
  149.             displayClassify($val, $interval+1);  
  150.             indentation($interval);  
  151.             echo ')<br/>';  
  152.         }else{  
  153.             echo ' => '.$val;  
  154.             if('id' == $key){  
  155.                 echo ' <a href="?action=showAdd&parent='.$val.'">增加子节点</a>';  
  156.                 echo ' <a href="?action=showModify&id='.$val.'">修改节点</a>';  
  157.                 echo ' <a href="?action=delete&id='.$val.'">删除节点</a>';  
  158.                 echo '<br/>';  
  159.                 indentation($interval);  
  160.                 getPath($val);  
  161.             }  
  162.             echo '<br/>';  
  163.         }  
  164.     }  
  165. }  
  166.   
  167. function indentation($interval){  
  168.     for($i=0;$i<$interval;$i++){  
  169.         echo '    ';  
  170.     }  
  171. }  
  172. //执行请求  
  173. $result = $action();  
  174.   
  175. if(false === $result){  
  176.     echo 'failed<br/>';  
  177.     echo $tree->getError();  
  178. }else{  
  179.     echo 'success';  
  180. }  

conf.php 文件

  1. <?php  
  2. return array(  
  3.         'dbHost'=>'localhost',  
  4.         'dbName'=>'classify_infinite',  
  5.         'dbUser'=>'root',  
  6.         'dbPass'=>'root',  
  7.         'dbCharset'=>'utf8'  
  8.     );  

DB.class.php  文件

  1. <?php  
  2. /** 
  3.  * 数据库操作类 
  4.  * 执行数据库的增删改查操作 
  5.  */  
  6. class DB {  
  7.     private $dbHost;//数据库主机名  
  8.     private $dbName;//数据库名称  
  9.     private $username;//数据库用户  
  10.     private $password;//数据库密码  
  11.     private $charset;//数据库字符集编码  
  12.     private $link;//数据库连接资源  
  13.     private $error = null;//错误信息  
  14.     /** 
  15.      * 构造函数 
  16.      * 根据传递进来的conf数组给局部变量赋初值 
  17.      * 连接数据库 
  18.      * @param array $conf 
  19.      */  
  20.     public function __construct($conf) {  
  21.         if(!isset($conf['dbHost']) || !isset($conf['dbName']) || !isset($conf['dbUser']) || !isset($conf['dbPass']) || !isset($conf['dbCharset'])){  
  22.             $this->error = '配置不完整';  
  23.         }else{  
  24.             $this->dbHost  = $conf['dbHost'];  
  25.             $this->dbName  = $conf['dbName'];  
  26.             $this->username = $conf['dbUser'];  
  27.             $this->password = $conf['dbPass'];  
  28.             $this->charset = $conf['dbCharset'];  
  29.             //连接数据库服务器  
  30.             $this->link = mysql_connect($this->dbHost,$this->username,$this->password);  
  31.             mysql_query("SET NAMES '{$this->charset}'");  
  32.             if(false === $this->link){  
  33.                 $this->error = '连接数据库失败';  
  34.             }elseif(!mysql_select_db($this->dbName,$this->link)){//选择数据库  
  35.                 $this->error = '连接数据库失败';  
  36.             }  
  37.         }  
  38.     }  
  39.     /** 
  40.      * 执行查询操作 
  41.      * @param string $sql   要执行的sql 
  42.      * @param [boolean] $key [如果标记为true,将结果以id为主键返回;如果标记为false,将结果以数字为主键返回] 
  43.      * @return boolean|array   如果执行出错,则记录错误,返回false;否则,返回查询得到的结果集 
  44.      */  
  45.     public function queryList($sql, $key=false){  
  46.         $resource = mysql_query($sql);  
  47.         if(false === $resource){  
  48.             $this->error = mysql_error();  
  49.             return false;  
  50.         }  
  51.         $result = array();  
  52.         while ($row = mysql_fetch_assoc($resource)) {  
  53.             if(false === $key){  
  54.                 array_push($result,$row);  
  55.             }else{  
  56.                 $result[$row['id']] = $row;  
  57.             }  
  58.         }  
  59.         return $result;  
  60.     }  
  61.     /** 
  62.      * [执行查询操作,返回一个结果数组] 
  63.      * @param  [string] $sql [要执行的sql] 
  64.      * @return [array|false]      [查询得到的结果集;如果查询出错,返回false] 
  65.      */  
  66.     public function queryOne($sql){  
  67.         $resource = mysql_query($sql);  
  68.         if(false === $resource){  
  69.             $this->error = mysql_error();  
  70.             return false;  
  71.         }  
  72.         $result = mysql_fetch_assoc($resource);  
  73.         return $result;  
  74.     }  
  75.     /** 
  76.      * 执行插入操作 
  77.      * @param string $sql   要执行的sql 
  78.      * @return boolean      如果执行出错,则记录错误并返回false;如果执行成功,则直接返回插入的数据的id 
  79.      */  
  80.     public function insert($sql){  
  81.         $result = mysql_query($sql);  
  82.         if(false === $result){  
  83.             $this->error = mysql_error();  
  84.             return false;  
  85.         }else{  
  86.             return mysql_insert_id();  
  87.         }  
  88.     }  
  89.     /** 
  90.      * 执行更新操作 
  91.      * @param string $sql   要执行的sql 
  92.      * @return boolean      如果执行出错,则记录错误信息并返回false;如果执行成功,则直接返回所影响的行数 
  93.      */  
  94.     public function update($sql){  
  95.         $result = mysql_query($sql);  
  96.         if(false === $result){  
  97.             $this->error = mysql_error();  
  98.             return false;  
  99.         }else{  
  100.             return mysql_affected_rows();  
  101.         }  
  102.     }  
  103.     /** 
  104.      * 执行删除操作 
  105.      * 调用更新操作来完成 
  106.      */  
  107.     public function delete($sql){  
  108.         return $this->update($sql);  
  109.     }  
  110.     /** 
  111.      * 获取错误信息 
  112.      * 如果错误信息不存在,则返回null 
  113.      * @return string   返回值;返回当前的错误信息 
  114.      */  
  115.     public function getError(){  
  116.         return $this->error;  
  117.     }  
  118.     public function __destruct(){  
  119.         mysql_close($this->link);  
  120.     }  
  121.     /** 
  122.      * 开启事务 
  123.      */  
  124.     public function begin()  
  125.     {  
  126.         mysql_query('begin');  
  127.     }  
  128.     /** 
  129.      * 事务回滚 
  130.      */  
  131.     public function rollBack()  
  132.     {  
  133.         mysql_query('rollback');  
  134.     }  
  135.     /** 
  136.      * 事务提交 
  137.      */  
  138.     public function submit()  
  139.     {  
  140.         mysql_query('commit');  
  141.     }  
  142. }  

ClassifyTree.class.php 文件

  1. <?php  
  2. class ClassifyTree  
  3. {  
  4.     private $db;  
  5.     private $error;  
  6.     public function __construct($conf){  
  7.         $this->db = new DB($conf);  
  8.         $this->error = $this->db->getError();  
  9.     }  
  10.     /** 
  11.      * [返回错误信息] 
  12.      * @return [string] [错误信息] 
  13.      */  
  14.     public function getError(){  
  15.         return $this->error;  
  16.     }  
  17.     /** 
  18.      * [获取整个分类树结构] 
  19.      * @return [array|boolean] [如果查询失败,则返回false;否则,返回格式化后的分类数组] 
  20.      */  
  21.     public function getAll(){  
  22.         $sql = 'SELECT `id`,`name`,`lft`,`rgt`,`parentId` FROM `classify`';  
  23.         $classifyInfo = $this->db->queryList($sql);  
  24.         if(false === $classifyInfo){  
  25.             $this->error = '查询出错。'.$this->db->getError();  
  26.             return false;  
  27.         }  
  28.         //格式化数组  
  29.         $result = $this->format($classifyInfo);  
  30.         return $result;  
  31.     }  
  32.     /** 
  33.      * [格式化检索到的分类数据库中的数据,将信息组织成 
  34.      * array( 
  35.      *      array(** 分类a **) 
  36.      *      'childrens'=>array( 
  37.      *              array(** 分类b **) 
  38.      *              'childrens'=>array( 
  39.      *                  ...... 
  40.      *              ) 
  41.      *          ) 
  42.      * ) 
  43.      * 的形式 
  44.      * ] 
  45.      * @param  [array]  $classifyInfo [需要格式化的数据] 
  46.      * @param  [integer] $parentId [父分类的id] 
  47.      * @return [array]    [格式化后的数据] 
  48.      */  
  49.     private function format(&$classifyInfo, $parentId=-1){  
  50.         $result = array();//需要返回的结果数组  
  51.         foreach($classifyInfo as $key=>$oneInfo){  
  52.             if($parentId == $oneInfo['parentId']){  
  53.                 $childrens = $this->format($classifyInfo, $oneInfo['id']);  
  54.                 if(!empty($childrens)){  
  55.                     $oneInfo['childrens'] = $childrens;  
  56.                 }  
  57.                 $result[] = $oneInfo;  
  58.             }  
  59.         }  
  60.         return $result;  
  61.     }  
  62.     /** 
  63.      * [获取某个分类到根分类的路径] 
  64.      * @param  [int] $id [分类id] 
  65.      * @return [array|boolean] [如果查询失败,则返回false;否则,返回路径数组] 
  66.      */  
  67.     public function getPath($id){  
  68.         //查询$id的分类和根分类的左右值  
  69.         $sql = 'SELECT `id`,`lft`,`rgt` FROM `classify` WHERE `id`='.$id.' or `parentId`=-1';  
  70.         $classifyInfo = $this->db->queryList($sql, true);  
  71.         if(false === $classifyInfo){  
  72.             $this->error = '查询失败:'.$this->db->getError();  
  73.             return false;  
  74.         }  
  75.         if(1 != count($classifyInfo)){  
  76.             $left = $classifyInfo[$id]['lft'];  
  77.             $right = $classifyInfo[$id]['rgt'];  
  78.             unset($classifyInfo[$id]);  
  79.             $classifyInfo = array_pop($classifyInfo);  
  80.             $rootLeft = $classifyInfo['lft'];  
  81.             $rootRight = $classifyInfo['rgt'];  
  82.         }else{  
  83.             $rootLeft = $left = $classifyInfo[$id]['lft'];  
  84.             $rootRight = $right = $classifyInfo[$id]['rgt'];  
  85.         }  
  86.         //查询当前节点到根节点的距离  
  87.         $sql = 'SELECT `id`,`name`,`lft`,`rgt`,`parentId` '  
  88.         .'FROM `classify` '  
  89.         .'WHERE `lft`>='.$rootLeft.' AND `lft`<='.$left.' AND `rgt`<='.$rootRight.' AND `rgt`>='.$right  
  90.         .' ORDER BY `lft` ';  
  91.         $classifyPath = $this->db->queryList($sql);  
  92.         if(false === $classifyPath){  
  93.             $this->error = '查询失败:'.$this->db->getError();  
  94.             return false;  
  95.         }  
  96.         return $classifyPath;  
  97.     }  
  98.     /** 
  99.      * [获取指定分类下的分类] 
  100.      * @param  [int] $id [分类id] 
  101.      * @return [array|boolean] [如果查询失败,则返回false;否则,返回格式化后的分类数组] 
  102.      */  
  103.     public function getOne($id){  
  104.         //查询$id分类的左右值  
  105.         $sql = 'SELECT `lft`,`rgt` FROM `classify` WHERE `id`='.$id;  
  106.         $oneInfo = $this->db->queryOne($sql);  
  107.         if(false === $classifyInfo){  
  108.             $this->error = '查询失败:'.$this->db->getError();  
  109.             return false;  
  110.         }  
  111.         $left = $oneInfo['lft'];  
  112.         $right = $oneInfo['rgt'];  
  113.         //查询该分类下的所有分类  
  114.         $sql = 'SELECT `id`,`name`,`lft`,`rgt`,`parentId` FROM `classify` WHERE `lft`>='.$left.' AND `rgt`<='.$right;  
  115.         $classifyInfo = $this->db->queryList($sql);  
  116.         if(false === $classifyPath){  
  117.             $this->error = '查询失败:'.$this->db->getError();  
  118.             return false;  
  119.         }  
  120.         //格式化数组  
  121.         $result = $this->format($classifyInfo);  
  122.         return $result;  
  123.     }  
  124.     /** 
  125.      * [在一个分类下添加子分类] 
  126.      * @param  [int] $parentId [父分类的id] 
  127.      * @param  [string] $name [增加的分类的名称] 
  128.      * @return [boolean] [增加成功,则返回true;否则,返回false] 
  129.      */  
  130.     public function insertNew($parentId, $name){  
  131.         $this->db->begin();  
  132.         //查询当前分类的左右值  
  133.         $sql = 'SELECT `lft` FROM `classify` WHERE `id`='.$parentId;  
  134.         $oneInfo = $this->db->queryOne($sql);  
  135.         if(false === $oneInfo){  
  136.             $this->error = '查询失败:'.$this->db->getError();  
  137.             $this->db->rollBack();  
  138.             return false;  
  139.         }  
  140.         $left = $oneInfo['lft'];  
  141.   
  142.         //将所有左值大于当前分类左值的分类左右值加2  
  143.         $sql = 'UPDATE `classify` SET `lft`=`lft`+2,`rgt`=`rgt`+2 WHERE `lft`>'.$left;  
  144.         $result = $this->db->update($sql);  
  145.         if(false === $result){  
  146.             $this->error = '更新节点左右值失败.'.$this->db->getError();  
  147.             $this->db->rollBack();  
  148.             return false;  
  149.         }  
  150.         //更新右节点大于当前分类左值的分类节点  
  151.         $sql = 'UPDATE `classify` SET `rgt`=`rgt`+2 WHERE `rgt`>'.$left.' AND `lft`<='.$left;  
  152.         $result = $this->db->update($sql);  
  153.         if(false === $result){  
  154.             $this->error = '更新节点右值失败.'.$this->db->getError();  
  155.             $this->db->rollBack();  
  156.             return false;  
  157.         }  
  158.         //插入新的节点  
  159.         $left += 1;  
  160.         $right = $left+1;  
  161.         $sql = "INSERT INTO `classify` VALUES(null, '{$name}', {$left}, {$right}, {$parentId})";  
  162.         $result = $this->db->insert($sql);  
  163.         if(false === $result){  
  164.             $this->error = '插入新节点失败.'.$this->db->getError();  
  165.             $this->db->rollBack();  
  166.             return false;  
  167.         }  
  168.         $this->db->submit();  
  169.         return true;  
  170.     }  
  171.     /** 
  172.      * [删除一个分类信息] 
  173.      * @param  [int] $id [分类id] 
  174.      * @return [boolean] [删除成功,则返回true;否则,返回true] 
  175.      */  
  176.     public function delete($id){  
  177.         $this->db->begin();  
  178.         //查询当前分类的左右值  
  179.         $sql = 'SELECT `lft`,`rgt` FROM `classify` WHERE `id`='.$id;  
  180.         $oneInfo = $this->db->queryOne($sql);  
  181.         if(false === $oneInfo){print_r(debug_backtrace());  
  182.             $this->error = '查询失败:'.$this->db->getError();  
  183.             $this->db->rollBack();  
  184.             return false;  
  185.         }  
  186.         $left   = $oneInfo['lft'];  
  187.         $right  = $oneInfo['rgt'];  
  188.         $dValue = $right - $left + 1;  
  189.         //删除当前分类及其子分类  
  190.         $sql = 'DELETE FROM `classify` WHERE `lft`>='.$left.' AND `rgt`<='.$right;  
  191.         $result  = $this->db->delete($sql);  
  192.         if(false === $result){  
  193.             $this->error = '删除失败:'.$this->db->getError();  
  194.             $this->db->rollBack();  
  195.             return false;  
  196.         }  
  197.         //将所有左值大于当前分类左值的分类左右值加2  
  198.         $sql = 'UPDATE `classify` SET `lft`=`lft`-'.$dValue.' , `rgt`=`rgt`-'.$dValue.' WHERE `lft`>'.$right;  
  199.         $result = $this->db->update($sql);  
  200.         if(false === $result){  
  201.             $this->error = '更新节点左值失败.'.$this->db->getError();  
  202.             $this->db->rollBack();  
  203.             return false;  
  204.         }  
  205.         //更新右节点大于当前分类左值的分类节点  
  206.         $sql = 'UPDATE `classify` SET `rgt`=`rgt`-'.$dValue.' WHERE `lft`<'.$left.' AND '.' `rgt`>'.$left;  
  207.         $result = $this->db->update($sql);  
  208.         if(false === $result){  
  209.             $this->error = '更新节点右值失败.'.$this->db->getError();  
  210.             $this->db->rollBack();  
  211.             return false;  
  212.         }  
  213.         $this->db->submit();  
  214.         return true;  
  215.     }  
  216.     /** 
  217.      * [根据id查询一个分类的信息] 
  218.      * @param  [int] $id [分类id] 
  219.      * @return [array|boolean] [查询失败,则返回false;否则,返回分类信息数组] 
  220.      */  
  221.     public function searchById($id){  
  222.         //查询当前分类的左右值  
  223.         $sql = 'SELECT `id`,`name`,`lft`,`rgt`,`parentId` FROM `classify` WHERE `id`='.$id;  
  224.         $oneInfo = $this->db->queryOne($sql);  
  225.         if(false === $oneInfo){  
  226.             $this->error = '查询失败:'.$this->db->getError();  
  227.             return false;  
  228.         }  
  229.         return $oneInfo;  
  230.     }  
  231.     /** 
  232.      * [保存修改过的分类信息] 
  233.      * @param  [string] $newName [分类的新名称] 
  234.      * @param  [int] $id [分类id] 
  235.      * @return [boolean] [如果修改成功,则返回true;否则,返回false] 
  236.      */  
  237.     public function modify($newName, $id){  
  238.         $sql = "UPDATE `classify` SET `name`='{$newName}' WHERE `id`={$id}";  
  239.         $result = $this->db->update($sql);  
  240.         if(false === $result){  
  241.             $this->error = '更新失败:'.$this->db->getError();  
  242.             return false;  
  243.         }  
  244.         return true;  
  245.     }  
  246. }  

例子程序链接:

左右值无限级分类

相关教程

mysql左右值无限分类原理及实现

mysql 无限级分类实现思路

预排序遍历树算法(非递归无限极分类算法)学习笔记



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多