欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

ThinkPHP 无限分类输出一维数组model

程序员文章站 2022-05-19 23:37:18
...
ThinkPHP 无限分类输出一维数组model
/*
* $tableName 表名
* $prent_me_str 父ID字段
* $order 排序字段
* Yi210.com
*/
namespace Admin\Model;
use Think\Model;
class TreeModel
{
private $Model;
private $prent_me_str = 'pid';
private $order = 'sort';

public function __construct($tableName)
{
$this->Model = M( $tableName );
}

public function getTree( $parent_id = 0, $level = 0 )
{
global $temp;
$order = $this->order;
if ( $order ) {
$data = $this->Model->where("$this->prent_me_str = '" . $parent_id . "'")->order("$order asc")->select();
} else {
$data = $this->Model->where("$this->prent_me_str = '" . $parent_id . "'")->select();
}
$str = $level ? '|--' : '';
$level++;
if ( !empty($data) ) {
foreach ($data as $key => $value) {
$l = $level - 1;
$value['str'] = str_repeat(' ', $l * 4) . $str;
$value['isprent'] = $this->isPrent( $value['id'] );
$temp[] = $value;
$this->getTree($value['id'], $level);
}
}
return $temp;
}

}

输出格式:
水果
|-- 苹果
|-- 青苹果
|-- 梨
个站:http://www.yi210.com/archives/948.html

QQ交流群:323540593

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