Home php教程 php手册 thinkphp实现左右值增、改、删操作类

thinkphp实现左右值增、改、删操作类

Jun 07, 2016 am 11:43 AM

新手,写了基本功能,未写排序等相关操作;求高手改进,
数据库:
thinkphp实现左右值增、改、删操作类
类库文件:
Category.class.php<?php <br /> /**<br> 应用基于thinkphp的左右值无限分类<br> **/<br> class Category<br> {<br>     //传入实化的对象【M('表名')】<br>     private $objCategory;<br>     //基础节点ID号<br>     public $intCurrentId;<br>     //设置制表符样式<br>     private $arrTabsStyle = array(<br>                             'indent' =&gt; '   ',<br>                             'process' =&gt; '├ ',<br>                             'end'=&gt;'└ '<br>                         );<br>     //构造函数初始化<br>     public function __construct($objCategory)<br>     {<br>         $this-&gt;objCategory = $objCategory;<br>     }<br>     //验证传入ID【大于0的数字】<br>     private function checkFun($intId)<br>     {<br>         //$intId优先验证<br>         if(isset($intId))<br>         {<br>             $this-&gt;intCurrentId = $intId;<br>             return true;<br>         }<br>         //如果$this-&gt;intCurrentId 已设置,验证<br>         else<br>         {<br>             if(isset($this-&gt;intCurrentId))<br>             {<br>                 return true;<br>             }<br>             else<br>             {<br>                 return false;<br>             }<br>         }<br>     }<br>     //根据ID号获取当前节点左右值<br>     private function setCurrentData($intId)<br>     {<br>         if(false == $this-&gt;checkFun($intId))<br>         {<br>             return false;<br>         }<br>         $map['id'] = $this-&gt;intCurrentId;<br>         return $this-&gt;objCategory-&gt;field(array('lft','rgt'))-&gt;where($map)-&gt;find();<br>     }<br>     /*<br>     *作用:<br>         设置输出列表数据的制表符样式<br>     *参数:<br>         $key:arrTabsStyle的KEY<br>         $value:arrTabsStyle的值<br>     */<br>     public function setTabStyle($key, $value = '')<br>     {<br>         if(isset($this-&gt;arrTabsStyle[$key]))<br>         {<br>             $this-&gt;arrTabsStyle[$key] = $value;<br>         }<br>     }<br>     /*<br>     *作用:<br>         根据ID号获取当前节点数据<br>     *参数:<br>         $intId:可设置的,需读取节点ID号<br>     */<br>     public function getCurrentData($intId)<br>     {<br>         if(false == $this-&gt;checkFun($intId))<br>         {<br>             return false;<br>         }<br>         $map['id'] = $this-&gt;intCurrentId;<br>         return $this-&gt;objCategory-&gt;field(array('id','title'))-&gt;where($map)-&gt;find();<br>     }<br>     /*<br>     *作用:<br>         获取当前节点的父节点数据<br>     *参数:<br>         $intId:需要读取节点的ID<br>     */<br>     public function getParentCategoryData($intId)<br>     {<br>         $arrRoot = $this-&gt;setCurrentData($intId);<br>         if($arrRoot)<br>         {<br>             $map['lft'] = array('LT', $arrRoot['lft']);<br>             $map['rgt'] = array('GT', $arrRoot['rgt']);<br>             return $this-&gt;objCategory-&gt;where($map)-&gt;find();<br>         }<br>         else<br>         {<br>             return false;<br>         }<br>     }<br>     /*<br>     *作用:<br>         获取ID下节点列表<br>     *参数:<br>         $intId:需要读取节点的父ID<br>         $intLevel:目录等级默认到100级<br>     */<br>     public function getCategoryList($intId = 1, $intLevel = 100)<br>     {<br>         //获取选定节点左右值,得出取值区间<br>         $arrRoot = $this-&gt;setCurrentData($intId);<br>         if($arrRoot)<br>         {<br>             //读取数据库符合条件的数据<br>             $map['lft'] = array('BETWEEN', array($arrRoot['lft'], $arrRoot['rgt']));<br>             $arrChildList = $this-&gt;objCategory-&gt;where($map)-&gt;order('lft')-&gt;select();<br>             //return $arrChildList;<br>             //对取出数据进行格式化<br>             $arrRight = array();<br>             foreach($arrChildList as $v)<br>             {<br>                 <br>                 if(count($arrRight))<br>                 {<br>                     while ($arrRight[count($arrRight) - 1]                      {<br>                         array_pop($arrRight);<br>                     }<br>                 }<br>                 //设置读取目录等级<br>                 if($intLevel &gt; count($arrRight))<br>                 {<br>                     $title = $v['title'];<br>                     //设置输出时的样式<br>                     if(count($arrRight))<br>                     {<br>                         $title = $this-&gt;arrTabsStyle['process'].$title;<br>                     }<br>                     $title = str_repeat($this-&gt;arrTabsStyle['indent'], count($arrRight)).$title;<br>                     $returnCategoryList[] = array('id'=&gt;$v['id'],'title'=&gt;$title,'lft'=&gt;$v['lft'],'rgt'=&gt;$v['rgt']);<br>                     $arrRight[]  = $v['rgt'];<br>                 }<br>             }<br>             return $returnCategoryList;<br>         }<br>         return false;<br>     }<br>     /*<br>     *作用:<br>         获取节点的子节点数<br>     *参数:<br>         $intId:需要读取节点的父ID<br>     */<br>     public function getCategoryCount($intId)<br>     {<br>         $arrRoot = $this-&gt;setCurrentData($intId);<br>         return ($arrRoot['rgt'] - $arrRoot['lft'] - 1) / 2;<br>     }<br>     /*<br>     *作用:<br>         添加节点<br>     *参数:<br>         $bolType:true添加到节点前面,false添加到节点尾部<br>         $intId:添加到的父节点<br>     */<br>     public function insertCategory($bolType = false, $intPid)<br>     {<br>         $data = I('param.');<br>         if(!isset($intPid))<br>         {<br>             $intPid = $data['pid'];<br>         }<br>         $arrRoot = $this-&gt;setCurrentData($intPid);<br>         if($arrRoot)<br>         {<br>             if($bolType)<br>             //true添加到节点前面<br>             {<br>                 $this-&gt;objCategory-&gt;where('rgt&gt;'.$arrRoot['lft'])-&gt;setInc('rgt',2);<br>                 $this-&gt;objCategory-&gt;where('lft&gt;'.$arrRoot['lft'])-&gt;setInc('lft',2);<br>                 //设置当前节点的左右值<br>                 $data['lft'] = $arrRoot['lft'] + 1;<br>                 $data['rgt'] = $arrRoot['lft'] + 2;<br>             }<br>             else<br>             //false添加到节点尾部<br>             {<br>                 $this-&gt;objCategory-&gt;where('rgt&gt;='.$arrRoot['rgt'])-&gt;setInc('rgt',2);<br>                 $this-&gt;objCategory-&gt;where('lft&gt;'.$arrRoot['rgt'])-&gt;setInc('lft',2);<br>                 $data['lft'] = $arrRoot['rgt'];<br>                 $data['rgt'] = $arrRoot['rgt'] + 1;<br>             }<br>             return $this-&gt;objCategory-&gt;add($data);<br>         }<br>         else<br>         {<br>             return false;<br>         }<br>     }<br>     /*<br>     *作用:<br>         删除节点<br>     *参数:<br>         $intId:被删除的节点ID<br>     */<br>     public function deleteCategory($intId)<br>     {<br>         $arrRoot = $this-&gt;setCurrentData($intId);<br>         if($arrRoot)<br>         {<br>             $ints = $arrRoot['rgt'] - $arrRoot['lft'] + 1;<br>             $map['lft'] = array('BETWEEN', array($arrRoot['lft'], $arrRoot['rgt']));<br>             $this-&gt;objCategory-&gt;where($map)-&gt;delete();<br>             $this-&gt;objCategory-&gt;where('lft&gt;'.$arrRoot['rgt'])-&gt;setDec('lft',$ints);<br>             $this-&gt;objCategory-&gt;where('rgt&gt;'.$arrRoot['rgt'])-&gt;setDec('rgt',$ints);<br>             return true;<br>         }<br>         else<br>         {<br>             return false;<br>         }<br>     }<br>     /*<br>     *作用:<br>         更新节点<br>     *参数:<br>         $intId:被删除的节点ID<br>     */<br>     public function updateCategory()<br>     {<br>         //读取POST数据存入数组<br>         $data = I('param.');<br>         //父ID等于子ID,直接跳出<br>         if($data['pid'] == $data['id']){return false;}<br>         //post.pid和当前父post.old相等说明未改变目录,不更新左右值<br>         if($data['pid'] !== $data['oldpid'])<br>         {<br> /**********************************【读取所需的相关值】********************************/<br>             //获取新的父节点的数据<br>             $arrParent = $this-&gt;setCurrentData($data['pid']);<br>             //取当前节点的数据<br>             $arrCurrent = $this-&gt;setCurrentData($data['id']);<br> /* 任务:删除节点 */<br> /**********************************【A-1:隔离数据】************************************/<br>             //将需要调整位置的左右值+100000<br>             $map['lft'] = array(<br>                         array('EGT', $arrCurrent['lft']),<br>                         array('ELT', $arrCurrent['rgt'])<br>                     );<br>             $this-&gt;objCategory-&gt;where($map)-&gt;setInc('lft',100000);<br>             //因为左值已更新,所以条件变化+100000<br>             $map['lft'] = array(<br>                         array('EGT', $arrCurrent['lft'] + 100000),<br>                         array('ELT', $arrCurrent['rgt'] + 100000)<br>                     );<br>             $this-&gt;objCategory-&gt;where($map)-&gt;setInc('rgt',100000);<br>             unset($map);<br> /**********************************【A-2:更新正常节点值】******************************/<br>             //获取隔离节点后续更新的步长值<br>             $intStep = $arrCurrent['rgt'] - $arrCurrent['lft'] + 1;<br>             //更新节点左右值<br>             $map['lft'] = array(<br>                         array('GT', $arrCurrent['rgt']),<br>                         array('LT', 100000)<br>                     );<br>             $this-&gt;objCategory-&gt;where($map)-&gt;setDec('lft',$intStep);<br>             unset($map);<br>             $map['rgt'] = array(<br>                         array('GT', $arrCurrent['rgt']),<br>                         array('LT', 100000)<br>                     );<br>             $this-&gt;objCategory-&gt;where($map)-&gt;setDec('rgt',$intStep);<br>             unset($map);<br> /* 完成:删除节点 */<br> /* 任务:更新节点 */<br> /**********************************【B-1:新父节目点提供下级节点的空间】****************/<br>             $map['lft'] = array(<br>                         array('GT', $arrParent['lft']),<br>                         array('LT', 100000)<br>                     );<br>             $this-&gt;objCategory-&gt;where($map)-&gt;setInc('lft',$intStep);<br>             unset($map);<br>             $map['rgt'] = array(<br>                         array('GT', $arrParent['lft']),<br>                         array('LT', 100000)<br>                     );<br>             $this-&gt;objCategory-&gt;where($map)-&gt;setInc('rgt',$intStep);<br>             unset($map);<br> /**********************************【B-2:将节点放入指定下级的空间】********************/<br>             //获取隔离节点后续更新的步长值<br>             $intStep = 100000 + ($arrCurrent['lft'] - ($arrParent['lft'] + 1));<br>             //更新左右值大于父节点左值的节点的左右值<br>             $map['lft'] = array(<br>                         array('EGT', $arrCurrent['lft'] + 100000),<br>                         array('ELT', $arrCurrent['rgt'] + 100000)<br>                     );<br>             $this-&gt;objCategory-&gt;where($map)-&gt;setDec('lft',$intStep);<br>             unset($map);<br>             $map['rgt'] = array(<br>                         array('EGT', $arrCurrent['lft'] + 100000),<br>                         array('ELT', $arrCurrent['rgt'] + 100000)<br>                     );<br>             $this-&gt;objCategory-&gt;where($map)-&gt;setDec('rgt',$intStep);<br>         }<br>         return $this-&gt;objCategory-&gt;where('id='.$data['id'])-&gt;setField('title', $data['title']);<br>     }<br> }<br> ?&gt;使用:<?php <br /> class CategoryAction extends CommonAction {<br>     private $objCG;<br>     public function _initialize(){<br>         //导入分类库<br>         import('@.ORG.Util.Category');<br>         $this-&gt;objCG = new Category(M('CommonCategory'));<br>     }<br>     //目录列表<br>     public function index($id = 1){<br>         $this-&gt;cateorylist = $this-&gt;objCG-&gt;getCategoryList($id);<br>         $this-&gt;display();<br>     }<br>     //添加目录<br>     public function add($id=1){<br>         $this-&gt;cateorylist = $this-&gt;objCG-&gt;getCategoryList($id);<br>         $this-&gt;display();<br>     }<br>     //编辑目录<br>     public function edit($id){<br>         if (!empty($id)){<br>             //输出所有的节点<br>             $this-&gt;cateorylist = $this-&gt;objCG-&gt;getCategoryList(1);<br>             //读取当前节点数据<br>             $vo = $this-&gt;objCG-&gt;getCurrentData($id);<br>             if ($vo){<br>                 //读取当前节点的父节点数据<br>                 $arrParent = $this-&gt;objCG-&gt;getParentCategoryData($id);<br>                 $vo['pid'] = $arrParent['id'];<br>                 $this-&gt;assign('vo',$vo);<br>                 $this-&gt;display();<br>             }else{<br>                 $this-&gt;error('数据不存在!');<br>             }<br>         }else{<br>             $this-&gt;error('数据不存在!');<br>         }<br>     }<br>     //添加目录:操作<br>     public function insert(){<br>         $list = $this-&gt;objCG-&gt;insertCategory();<br>         if ($list !== false)<br>         {<br>             $this-&gt;success('数据保存成功!');<br>         }<br>         else<br>         {<br>             $this-&gt;error('数据写入错误!');<br>         }<br>     }<br>     public function delete($id){<br>         if (!empty($id))<br>         {<br>             $result = $this-&gt;objCG-&gt;deleteCategory($id);<br>             if ($result)<br>             {<br>                 $this-&gt;success('删除成功!');<br>             }<br>             else<br>             {<br>                 $this-&gt;error('删除出错!');<br>             }<br>         }<br>         else<br>         {<br>             $this-&gt;error('ID错误!');<br>         }<br>     }<br>     public function update(){<br>         $list = $this-&gt;objCG-&gt;updateCategory();<br>         if ($list !== false)<br>         {<br>             $this-&gt;success('更新成功!');<br>         }<br>         else<br>         {<br>             $this-&gt;error("操作失败!");<br>         }<br>     }<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

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 Article Tags

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)

Learn about introductory code examples for Python programming Learn about introductory code examples for Python programming Jan 04, 2024 am 10:50 AM

Learn about introductory code examples for Python programming

PHP variables in action: 10 real-life examples of use PHP variables in action: 10 real-life examples of use Feb 19, 2024 pm 03:00 PM

PHP variables in action: 10 real-life examples of use

How to use PHP to write inventory management function code in the inventory management system How to use PHP to write inventory management function code in the inventory management system Aug 06, 2023 pm 04:49 PM

How to use PHP to write inventory management function code in the inventory management system

From beginner to proficient: Code implementation of commonly used data structures in Go language From beginner to proficient: Code implementation of commonly used data structures in Go language Mar 04, 2024 pm 03:09 PM

From beginner to proficient: Code implementation of commonly used data structures in Go language

Java implements simple bubble sort code Java implements simple bubble sort code Jan 30, 2024 am 09:34 AM

Java implements simple bubble sort code

Go language programming examples: code examples in web development Go language programming examples: code examples in web development Mar 04, 2024 pm 04:54 PM

Go language programming examples: code examples in web development

Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Jul 05, 2023 pm 09:57 PM

Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces

Guidance and Examples: Learn to implement the selection sort algorithm in Java Guidance and Examples: Learn to implement the selection sort algorithm in Java Feb 18, 2024 am 10:52 AM

Guidance and Examples: Learn to implement the selection sort algorithm in Java

See all articles