Home  >  Article  >  php教程  >  THINKPHP3.2.3含有树形结构的无限分类的实现

THINKPHP3.2.3含有树形结构的无限分类的实现

PHP中文网
PHP中文网Original
2016-05-26 08:19:091585browse

THINKPHP3.2.3含有树形结构的无限分类的实现

<?php
namespace CommonHelper;

class Category {

//一维数组
 static public function toLevel($cate, $delimiter = &#39;———&#39;, $parent_id = 0, $level = 0) {

 $arr = array();
 foreach ($cate as $v) {
 if ($v[&#39;parent_id&#39;] == $parent_id) {
 $v[&#39;level&#39;] = $level + 1;
 $v[&#39;delimiter&#39;] = str_repeat($delimiter, $level);
 $arr[] = $v;
 $arr = array_merge($arr, self::toLevel($cate, $delimiter, $v[&#39;id&#39;], $v[&#39;level&#39;]));
}
}

 return $arr;

}


//组成多维数组
 static public function toLayer($cate, $name = &#39;child&#39;, $pid = 0){

 $arr = array();
 foreach ($cate as $v) {
 if ($v[&#39;pid&#39;] == $pid) {
 $v[$name] = self::toLayer($cate, $name, $v[&#39;id&#39;]);
 $arr[] = $v;
}
}

 return $arr;
}


 //一维数组(同模型)(model = tablename相同),删除其他模型的分类
 static public function getLevelOfModel($cate, $tablename = &#39;article&#39;) {

 $arr = array();
 foreach ($cate as $v) {
 if ($v[&#39;tablename&#39;] == $tablename) {
 $arr[] = $v;
}
}

 return $arr;

}

//一维数组(同模型)(modelid),删除其他模型的分类
 static public function getLevelOfModelId($cate, $modelid = 0) {

 $arr = array();
 foreach ($cate as $v) {
 if ($v[&#39;modelid&#39;] == $modelid) {
 $arr[] = $v;
}
}

 return $arr;

}

//传递一个子分类ID返回他的所有父级分类
 static public function getParents($cate, $id) {
 $arr = array();
 foreach ($cate as $v) {
 if ($v[&#39;id&#39;] == $id) {
 $arr[] = $v;
 $arr = array_merge(self::getParents($cate, $v[&#39;pid&#39;]), $arr);
}
}
 return $arr;
}

//传递一个子分类ID返回他的同级分类
 static public function getSameCate($cate, $id) {
 $arr = array();
 $self = self::getSelf($cate, $id);
 if (empty($self)) {
 return $arr;
}

 foreach ($cate as $v) {
 if ($v[&#39;id&#39;] == $self[&#39;pid&#39;]) {
 $arr[] = $v;
}
}
 return $arr;
}



//判断分类是否有子分类,返回false,true
 static public function hasChild($cate, $id) {
 $arr = false;
 foreach ($cate as $v) {
 if ($v[&#39;pid&#39;] == $id) {
 $arr = true;
 return $arr;
}
}

 return $arr;
}

//传递一个父级分类ID返回所有子分类ID
/**
 *@param $cate 全部分类数组
 *@param $pid 父级ID
 *@param $flag 是否包括父级自己的ID,默认不包括
**/
 static public function getChildsId($cate, $pid, $flag = 0) {
 $arr = array();
 if ($flag) {
 $arr[] = $pid;
}
 foreach ($cate as $v) {
 if ($v[&#39;pid&#39;] == $pid) {
 $arr[] = $v[&#39;id&#39;];
 $arr = array_merge($arr , self::getChildsId($cate, $v[&#39;id&#39;]));
}
}

 return $arr;
}


//传递一个父级分类ID返回所有子级分类
 static public function getChilds($cate, $pid) {
 $arr = array();
 foreach ($cate as $v) {
 if ($v[&#39;pid&#39;] == $pid) {
 $arr[] = $v;
 $arr = array_merge($arr, self::getChilds($cate, $v[&#39;id&#39;]));
}
}
 return $arr;
}

//传递一个分类ID返回该分类相当信息
 static public function getSelf($cate, $id) {
 $arr = array();
 foreach ($cate as $v) {
 if ($v[&#39;id&#39;] == $id) {
 $arr = $v;
 return $arr;
}
}
 return $arr;
}

//传递一个分类ID返回该分类相当信息
 static public function getSelfByEName($cate, $ename) {
 $arr = array();
 foreach ($cate as $v) {
 if ($v[&#39;ename&#39;] == $ename) {
 $arr = $v;
 return $arr;
}
}
 return $arr;
}

}

?>
<?php
namespace AdminController;
use ThinkController;
use CommonHelperCategory;
class CategoryController extends AuthController {
//栏目列表页
 public function index(){
 $category_list = M(&#39;category&#39;)->order(&#39;sort desc&#39;)->select();
 $category_list = Category::toLevel($category_list, &#39;    &#39;,0);

 $this->assign(&#39;category_list&#39;, $category_list);
$this->display(&#39;category_list&#39;);
}

 //增加 栏目页面
 public function add(){
 $parent_id = I(&#39;parent_id&#39;, 0);

 $parent_category_info = M(&#39;Category&#39;)->where(array(&#39;id&#39; => $parent_id))->find();

if($parent_category_info){
 $this->assign(&#39;parent_id&#39;, $parent_id);
 $this->assign(&#39;parent_category_info&#39;, $parent_category_info);
}


 $model_list = M(&#39;Model&#39;)->where(array(&#39;status&#39; => 1))->order(&#39;sort asc&#39;)->select();

 $category_list = M(&#39;category&#39;)->order(&#39;sort desc&#39;)->select();
 $category_list = Category::toLevel($category_list, &#39;---&#39;,0);

 $this->assign(&#39;category_list&#39;, $category_list);
 $this->assign(&#39;action&#39;, &#39;add&#39;);
 $this->assign(&#39;model_list&#39;, $model_list);
$this->display(&#39;category_add&#39;);
}


//修改栏目
 public function edit(){
 $category_id = I(&#39;id&#39;);
 $category_info = M(&#39;Category&#39;)->find($category_id);
 $model_list = M(&#39;Model&#39;)->order(&#39;sort desc&#39;)->select();

 $category_list = M(&#39;category&#39;)->order(&#39;sort desc&#39;)->select();
 $category_list = Category::toLevel($category_list, &#39;---&#39;,0);

 $this->assign(&#39;category_list&#39;, $category_list);
 $this->assign(&#39;action&#39;, &#39;edit&#39;);
 $this->assign(&#39;category_id&#39;, $category_id);
 $this->assign(&#39;category_info&#39;, $category_info);
 $this->assign(&#39;model_list&#39;, $model_list);

$this->display(&#39;category_add&#39;);
}

//保存栏目
 public function save(){
 $Category = D("Category");
 if (!$Category->create()){
 //验证没有通过 输出错误提示信息
 $errormsg = $Category->getError();
$this->error($errormsg);
}else{
 // 验证通过进行其他数据操作
 $id = I(&#39;id&#39;, 0);

 $data[&#39;title&#39;] = I(&#39;title&#39;, &#39;&#39;);
 $data[&#39;flag&#39;] = I(&#39;flag&#39;, &#39;&#39;);
 $data[&#39;parent_id&#39;] = I(&#39;parent_id&#39;, 0);
 $data[&#39;content&#39;] = I(&#39;content&#39;, &#39;&#39;);
 $data[&#39;sort&#39;] = I(&#39;sort&#39;, 0);
 $data[&#39;status&#39;] = I(&#39;status&#39;, 1);
 $data[&#39;is_show&#39;] = I(&#39;is_show&#39;, 1);
 $data[&#39;has_cover&#39;] = I(&#39;has_cover&#39;, 1);
 $data[&#39;cover&#39;] = I(&#39;cover&#39;, &#39;&#39;);

 $category_info = $Category->find($id);
if($category_info){
//更新操作
 $data[&#39;update_time&#39;] = time();

 $result = $Category->where(array(&#39;id&#39; => $id))->save($data);
 if(false !== $result || 0 !== $result){
 $this->success(&#39;栏目更新成功&#39;, U(&#39;Admin/Category/index&#39;));
}else{
$this->error(&#39;栏目更新失败&#39;);
}
}else{
 $data[&#39;model_id&#39;] = I(&#39;model_id&#39;, 0);
 $data[&#39;model_flag&#39;] = M(&#39;Model&#39;)->where(array(&#39;id&#39; => I(&#39;model_id&#39;)))->getField(&#39;flag&#39;);
 $data[&#39;add_time&#39;] = time();

//print_r($data);exit;

 $result = $Category->add($data);
if($result){
 $this->success(&#39;栏目添加成功&#39;, U(&#39;Admin/Category/index&#39;));
}else{
$this->error(&#39;栏目添加失败&#39;);
}
}
}
}

//更新状态
 public function change_status()
{
 $id = I(&#39;id&#39;);

 $Category = M(&#39;Category&#39;);
 $category_info = $Category->find($id);
if(!$category_info){
 $this->ajaxReturn(array(&#39;info&#39; => &#39;该栏目不存在&#39;));
}else{
if($category_info[&#39;status&#39;]){
 $status = 0;
}else{
 $status = 1;
}
 $result = $Category->where(array(&#39;id&#39; => $id))->setField(&#39;status&#39;,$status);
 if(false != $result){
 $this->ajaxReturn(array(&#39;status&#39; => &#39;ok&#39;, &#39;info&#39; => &#39;栏目状态更新成功&#39;));
}else{
 $this->ajaxReturn(array(&#39;info&#39; => &#39;栏目状态更新失败&#39;));
}
}

}

//保存排序
 public function save_sort()
{
 $id = I(&#39;id&#39;);
 $sort = I(&#39;sort&#39;);

 $Category = M(&#39;Category&#39;);
 $result = $Category->where(array(&#39;id&#39; => $id))->setField(&#39;sort&#39;,$sort);
 if(false !== $result || 0 !== $result){
 $this->ajaxReturn(array(&#39;status&#39; => &#39;ok&#39;, &#39;info&#39; => &#39;排序更新成功&#39;));
}else{
 $this->ajaxReturn(array(&#39;info&#39; => &#39;排序更新失败&#39;));
}
}

//删除栏目
 public function delete()
{
 $id = I(&#39;id&#39;);

 $Category = M(&#39;Category&#39;);
 $category_info = $Category->find($id);
if(!$category_info){
 return array(&#39;info&#39; => &#39;该模型不存在&#39;);
 }else if($category_info[&#39;is_system&#39;]){
 return array(&#39;info&#39; => &#39;系统模型不允许删除&#39;);
}else{
 $result = $Category->delete($id);
if($result){
 $this->ajaxReturn(array(&#39;status&#39; => &#39;ok&#39;, &#39;info&#39; => &#39;栏目删除成功&#39;));
}else{
 $this->ajaxReturn(array(&#39;info&#39; => &#39;栏目删除失败&#39;));
}
}
}

}
Statement:
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