mysql和php無限分類 資料歸類 輸出

2021-08-14 00:11:30 字數 2035 閱讀 8607

資料歸類解釋:利用pid(子級)=id(父級);讓子級迴圈的時自己去找父級在哪兒,然後加入其中;

資料表結構:

create table `newtable` (

`id` int(10) unsigned not null auto_increment ,

`pid` int(10) unsigned not null ,

`name` varchar(255) character set utf8 collate utf8_general_ci null default '' ,

primary key (`id`),

index `pid` (`pid`) using btree

)engine=innodb

default character set=latin1 collate=latin1_swedish_ci

auto_increment=1

row_format=compact;

源資料

insert into `test1` values (1, 0, 'menu1');

insert into `test1` values (2, 0, 'menu2');

insert into `test1` values (3, 0, 'menu3');

insert into `test1` values (4, 0, 'menu4');

insert into `test1` values (5, 0, 'menu5');

insert into `test1` values (6, 0, 'menu6');

insert into `test1` values (7, 1, 'menu1-1');

insert into `test1` values (8, 1, 'menu1-2');

insert into `test1` values (9, 1, 'menu1-3');

insert into `test1` values (10, 7, 'menu7-1');

insert into `test1` values (11, 7, 'menu7-2');

insert into `test1` values (12, 7, 'menu7-3');

insert into `test1` values (13, 7, 'menu7-4');

insert into `test1` values (14, 10, 'menu10-1');

insert into `test1` values (15, 10, 'menu10-2');

insert into `test1` values (16, 10, 'menu10-3');

insert into `test1` values (17, 8, 'menu8-1');

php**:基於tp5框架

<?php

use think\db;

use think\debug;

class index

//將有pid的元素,整合到對應的pid=id(這一步是最重要的)

foreach ($tree as $key => $val)

}//去除tree中有pid的元素,因為他們已經被歸類了;

foreach ($tree as $key => $val)

$temp = array();

$grup = '';

$flag = '|--';

$this->recursive($tree,$temp,$grup,$flag);

var_dump($temp);exit;

}public function recursive($data,&$temp,&$division,$flag)

}$division = $divit;

return false;

}}

新陣列輸出如下:

array(17)

PHP無限分類

定義乙個陣列 或者從資料庫裡取出 data array array id 1,pid 0,cname 數碼 array id 2,pid 0,cname 家用電器 array id 3,pid 0,cname 電腦 array id 4,pid 0,cname 服裝 array id 5,pid 0...

PHP遞迴寫入MySQL無限級分類資料

php遞迴寫入mysql無限級分類資料,表結構 create table kepler goods category id int unsigned not null primary key auto increment,parentid int unsigned not null default ...

php實現無限分類

access public param pid 節點的id param array 返回該節點的所有後代節點 public function list cate pid 0 access private param arr array 要遍歷的陣列 param pid 節點的pid,預設為0,表示從...