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' => ' ',<br>
'process' => '├ ',<br>
'end'=>'└ '<br>
);<br>
//构造函数初始化<br>
public function __construct($objCategory)<br>
{<br>
$this->objCategory = $objCategory;<br>
}<br>
//验证传入ID【大于0的数字】<br>
private function checkFun($intId)<br>
{<br>
//$intId优先验证<br>
if(isset($intId))<br>
{<br>
$this->intCurrentId = $intId;<br>
return true;<br>
}<br>
//如果$this->intCurrentId 已设置,验证<br>
else<br>
{<br>
if(isset($this->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->checkFun($intId))<br>
{<br>
return false;<br>
}<br>
$map['id'] = $this->intCurrentId;<br>
return $this->objCategory->field(array('lft','rgt'))->where($map)->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->arrTabsStyle[$key]))<br>
{<br>
$this->arrTabsStyle[$key] = $value;<br>
}<br>
}<br>
/*<br>
*作用:<br>
根据ID号获取当前节点数据<br>
*参数:<br>
$intId:可设置的,需读取节点ID号<br>
*/<br>
public function getCurrentData($intId)<br>
{<br>
if(false == $this->checkFun($intId))<br>
{<br>
return false;<br>
}<br>
$map['id'] = $this->intCurrentId;<br>
return $this->objCategory->field(array('id','title'))->where($map)->find();<br>
}<br>
/*<br>
*作用:<br>
获取当前节点的父节点数据<br>
*参数:<br>
$intId:需要读取节点的ID<br>
*/<br>
public function getParentCategoryData($intId)<br>
{<br>
$arrRoot = $this->setCurrentData($intId);<br>
if($arrRoot)<br>
{<br>
$map['lft'] = array('LT', $arrRoot['lft']);<br>
$map['rgt'] = array('GT', $arrRoot['rgt']);<br>
return $this->objCategory->where($map)->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->setCurrentData($intId);<br>
if($arrRoot)<br>
{<br>
//读取数据库符合条件的数据<br>
$map['lft'] = array('BETWEEN', array($arrRoot['lft'], $arrRoot['rgt']));<br>
$arrChildList = $this->objCategory->where($map)->order('lft')->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 > count($arrRight))<br>
{<br>
$title = $v['title'];<br>
//设置输出时的样式<br>
if(count($arrRight))<br>
{<br>
$title = $this->arrTabsStyle['process'].$title;<br>
}<br>
$title = str_repeat($this->arrTabsStyle['indent'], count($arrRight)).$title;<br>
$returnCategoryList[] = array('id'=>$v['id'],'title'=>$title,'lft'=>$v['lft'],'rgt'=>$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->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->setCurrentData($intPid);<br>
if($arrRoot)<br>
{<br>
if($bolType)<br>
//true添加到节点前面<br>
{<br>
$this->objCategory->where('rgt>'.$arrRoot['lft'])->setInc('rgt',2);<br>
$this->objCategory->where('lft>'.$arrRoot['lft'])->setInc('lft',2);<br>
//设置当前节点的左右值<br>
$data['lft'] = $arrRoot['lft'] + 1;<br>
$data['rgt'] = $arrRoot['lft'] + 2;<br>
}<br>
else<br>
//false添加到节点尾部<br>
{<br>
$this->objCategory->where('rgt>='.$arrRoot['rgt'])->setInc('rgt',2);<br>
$this->objCategory->where('lft>'.$arrRoot['rgt'])->setInc('lft',2);<br>
$data['lft'] = $arrRoot['rgt'];<br>
$data['rgt'] = $arrRoot['rgt'] + 1;<br>
}<br>
return $this->objCategory->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->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->objCategory->where($map)->delete();<br>
$this->objCategory->where('lft>'.$arrRoot['rgt'])->setDec('lft',$ints);<br>
$this->objCategory->where('rgt>'.$arrRoot['rgt'])->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->setCurrentData($data['pid']);<br>
//取当前节点的数据<br>
$arrCurrent = $this->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->objCategory->where($map)->setInc('lft',100000);<br>
//因为左值已更新,所以条件变化+100000<br>
$map['lft'] = array(<br>
array('EGT', $arrCurrent['lft'] + 100000),<br>
array('ELT', $arrCurrent['rgt'] + 100000)<br>
);<br>
$this->objCategory->where($map)->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->objCategory->where($map)->setDec('lft',$intStep);<br>
unset($map);<br>
$map['rgt'] = array(<br>
array('GT', $arrCurrent['rgt']),<br>
array('LT', 100000)<br>
);<br>
$this->objCategory->where($map)->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->objCategory->where($map)->setInc('lft',$intStep);<br>
unset($map);<br>
$map['rgt'] = array(<br>
array('GT', $arrParent['lft']),<br>
array('LT', 100000)<br>
);<br>
$this->objCategory->where($map)->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->objCategory->where($map)->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->objCategory->where($map)->setDec('rgt',$intStep);<br>
}<br>
return $this->objCategory->where('id='.$data['id'])->setField('title', $data['title']);<br>
}<br>
}<br>
?>
使用:<?php <br />
class CategoryAction extends CommonAction {<br>
private $objCG;<br>
public function _initialize(){<br>
//导入分类库<br>
import('@.ORG.Util.Category');<br>
$this->objCG = new Category(M('CommonCategory'));<br>
}<br>
//目录列表<br>
public function index($id = 1){<br>
$this->cateorylist = $this->objCG->getCategoryList($id);<br>
$this->display();<br>
}<br>
//添加目录<br>
public function add($id=1){<br>
$this->cateorylist = $this->objCG->getCategoryList($id);<br>
$this->display();<br>
}<br>
//编辑目录<br>
public function edit($id){<br>
if (!empty($id)){<br>
//输出所有的节点<br>
$this->cateorylist = $this->objCG->getCategoryList(1);<br>
//读取当前节点数据<br>
$vo = $this->objCG->getCurrentData($id);<br>
if ($vo){<br>
//读取当前节点的父节点数据<br>
$arrParent = $this->objCG->getParentCategoryData($id);<br>
$vo['pid'] = $arrParent['id'];<br>
$this->assign('vo',$vo);<br>
$this->display();<br>
}else{<br>
$this->error('数据不存在!');<br>
}<br>
}else{<br>
$this->error('数据不存在!');<br>
}<br>
}<br>
//添加目录:操作<br>
public function insert(){<br>
$list = $this->objCG->insertCategory();<br>
if ($list !== false)<br>
{<br>
$this->success('数据保存成功!');<br>
}<br>
else<br>
{<br>
$this->error('数据写入错误!');<br>
}<br>
}<br>
public function delete($id){<br>
if (!empty($id))<br>
{<br>
$result = $this->objCG->deleteCategory($id);<br>
if ($result)<br>
{<br>
$this->success('删除成功!');<br>
}<br>
else<br>
{<br>
$this->error('删除出错!');<br>
}<br>
}<br>
else<br>
{<br>
$this->error('ID错误!');<br>
}<br>
}<br>
public function update(){<br>
$list = $this->objCG->updateCategory();<br>
if ($list !== false)<br>
{<br>
$this->success('更新成功!');<br>
}<br>
else<br>
{<br>
$this->error("操作失败!");<br>
}<br>
}<br>
}
AD:真正免费,域名+虚机+企业邮箱=0元

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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

Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

PHP variables store values during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above
