ThinkPHP 无限分类输出一维数组model<?php<br />
/*<br />
* $tableName 表名<br />
* $prent_me_str 父ID字段<br />
* $order 排序字段<br />
* Yi210.com<br />
*/<br />
namespace AdminModel;<br />
use ThinkModel;<br />
class TreeModel<br />
{<br />
private $Model;<br />
private $prent_me_str = 'pid';<br />
private $order = 'sort';<br />
<br />
public function __construct($tableName)<br />
{<br />
$this->Model = M( $tableName );<br>
}<br>
<br>
public function getTree( $parent_id = 0, $level = 0 )<br>
{<br>
global $temp;<br>
$order = $this->order;<br>
if ( $order ) {<br>
$data = $this->Model->where("$this->prent_me_str = '" . $parent_id . "'")->order("$order asc")->select();<br>
} else {<br>
$data = $this->Model->where("$this->prent_me_str = '" . $parent_id . "'")->select();<br>
}<br>
$str = $level ? '|--' : '';<br>
$level ;<br>
if ( !empty($data) ) {<br>
foreach ($data as $key => $value) {<br>
$l = $level - 1;<br>
$value['str'] = str_repeat(' ', $l * 4) . $str;<br>
$value['isprent'] = $this->isPrent( $value['id'] );<br>
$temp[] = $value;<br>
$this->getTree($value['id'], $level);<br>
}<br>
}<br>
return $temp;<br>
}<br>
<br>
}<br>
<br>
输出格式:<br>
水果<br>
|-- 苹果<br>
|-- 青苹果<br>
|-- 梨
个站:http://www.yi210.com/archives/948.html
QQ交流群:323540593
AD:真正免费,域名 虚机 企业邮箱=0元