1.方式一
namespace app\index\model;
use think\Model;
class Classfiy extends Model{
protected $table = 'classfiy';
// 方式一
public function htmllist(){
$data=$this->select();
return $this->htmllistM($data);
}
public function htmllistM($data,$pid=0,$lev=''){
static $arr=array();
foreach($data as $key=>$value){
if($value['pid']==$pid){
$value['lev']=$lev;
$arr[]=$value;
$this->htmllistM($data,$value['id'],$lev.'!--');
}
}
return $arr;
}
// 方式二
public function getHtmlList(&$result = [], $pid = 0, $prefx = '')
{
$data = $this->select();
// $data = $this->select()->toarray();//有时出错
foreach ($data as $value) {
if ($value['pid'] == $pid) {
$value['name'] = $prefx.$value['name'];
$result[] = $value;
$this->getHtmlList($result,$value['id'],$prefx.'|--');
}
}
return $result;
}
}
?>
方式一返回的数据形式是
{
"ret": 200,
"data": [
{
"id": 1,
"name": "手机",
"pid": 0,
"static": 1,
"time": 1588775166,
"last_time": null,
"delete_time": null,
"lev": ""
},
{
"id": 4,
"name": "华为手机",
"pid": 1,
"static": 1,
"time": 1588776159,
"last_time": null,
"delete_time": null,
"lev": "!--"
},
{
"id": 6,
"name": "苹果手机",
"pid": 1,
"static": 1,
"time": 1588776200,
"last_time": null,
"delete_time": null,
"lev": "!--"
},
{
"id": 2,
"name": "电脑",
"pid": 0,
"static": 0,
"time": 1588775190,
"last_time": null,
"delete_time": null,
"lev": ""
},
{
"id": 5,
"name": "华为电脑",
"pid": 2,
"static": 0,
"time": 1588776185,
"last_time": 1588816674,
"delete_time": null,
"lev": "!--"
},
{
"id": 7,
"name": "苹果电脑",
"pid": 2,
"static": 1,
"time": 1588776215,
"last_time": 1588816980,
"delete_time": null,
"lev": "!--"
}
],
"msg": ""}
方式二返回的数据 格式是
{
"ret": 200,
"data": [
"{
"id": 1,
"name": "mobile phone",
"pid" ": 0,
"static": 1,
"time": 1588775166,
"last_time": null,
"delete_time": null
},
{
"ID": 4,
"name": "|-Huawei mobile phone",
"pid": 1,
"Static": 1,
"Time": 1588776159,
"last_time": null,
"delete_time": null
},
"id": 6,
"name": "|--iPhone",
"pid": 1,
"static": 1,
"time": 1588776200,
"last_time": null,
"delete_time": null
},
"id": 2,
"name": "computer",
"pid": 0,
"static": 0,
"time": 1588 775190,
"last_time": null,
"delete_time": null
},
"id": 5,
"name": "|--Huawei Computer",
"pid": 2,
"static": 0,
"time": 1588776185,
"last_time": 1588816674,
" delete_time": null
},
"id": 7,
"id": 7, : 1588776215,
"last_time": 1588816980,
"delete_time": null
}
],
"msg": ""
}
The above is the detailed content of Unlimited classification of thinkphp5 data. For more information, please follow other related articles on the PHP Chinese website!