w



w

1 $res = array();
foreach($idlist_1 as $id1)
{
$tmp = array();
$tmp1 = array();
$tmp1[] = $id1;
foreach($idlist_2 as $id2)
{
$tmp2 = array();
if(get_parentid($id2)==$id1)
{
$tmp2[] = $id2;
$tmp3 = array();
foreach($idlist_3 as $id3)
{
if(get_parentid($id3)==$id2) $tmp3[]=$id3;
}
$tmp2[] = $tmp3;
$tmp1[] = $tmp2;
}
}
$res[] = $tmp1;
}
 Array
(
[0] => Array
(
[0] => 1
[1] => Array
(
[0] => 4
[1] => Array
(
[0] => 13
[1] => 14
[2] => 15
) ) [2] => Array
(
[0] => 5
[1] => Array
(
[0] => 16
[1] => 17
[2] => 18
) ) [3] => Array
(
[0] => 6
[1] => Array
(
[0] => 19
[1] => 20
[2] => 21
) ) ) [1] => Array
(
[0] => 2
[1] => Array
(
[0] => 7
[1] => Array
(
[0] => 22
[1] => 23
[2] => 24
) ) [2] => Array
(
[0] => 8
[1] => Array
(
[0] => 25
[1] => 26
[2] => 27
) ) [3] => Array
(
[0] => 9
[1] => Array
(
[0] => 28
[1] => 29
[2] => 30
) ) ) [2] => Array
(
[0] => 3
[1] => Array
(
[0] => 10
[1] => Array
(
[0] => 31
[1] => 32
[2] => 33
) ) [2] => Array
(
[0] => 11
[1] => Array
(
[0] => 34
[1] => 35
[2] => 36
) ) [3] => Array
(
[0] => 12
[1] => Array
(
[0] => 37
[1] => 38
[2] => 39
) ) ) )

w

php利用递归函数实现无限级分类 - 杰枫Jeff - 博客园
http://www.cnblogs.com/DeanChopper/p/4706071.html

create table onepiece(
id int auto_increment,
pid int not null,
name varchar(225) not null,
primary key(id)
); insert onepiece values
(1,0,'0_0'),
(2,0,'0_1'),
(3,0,'0_2'),
(4,1,'0_0_0'),
(5,1,'0_0_1'),
(6,1,'0_0_2'),
(7,2,'0_1_0'),
(8,2,'0_1_1'),
(9,2,'0_1_2'),
(10,9,'0_1_2_0'),
(11,7,'0_1_0_0'),
(12,8,'0_1_1_0'),
(13,8,'0_1_1_1');
<?php

class Unlimited
{
protected $mysqli; public function __construct($config)
{
$this->mysqli = new mysqli($config['host'], $config['user'], $config['pwd']);
$this->mysqli->select_db($config['db']);
$this->mysqli->set_charset('utf8');
if ($this->mysqli->connect_errno) {
echo $this->mysqli->connect_error;
}
} private function getList($pid = 0, &$result = array(), $spac = 0)
{
$spac = $spac + 2;
$sql = "select * from onepiece where pid={$pid}";
$rs = $this->mysqli->query($sql);
while ($row = $rs->fetch_assoc()) {
$row['name'] = str_repeat('&nbsp;&nbsp', $spac) . $row['name'];
$result[] = $row;
$this->getList($row['id'], $result, $spac);
}
return $result;
} /**
* 展现下拉列表式分类
* @return [type]
*/
public function displayList()
{
$rs = $this->getList();
$str = "<select name='cate'>"; foreach ($rs as $key => $val) {
$str .= "<option >{$val['name']}</option>";
}
$str .= "</select>";
return $str;
} private function getLink($cid, &$result = array())
{
$sql = "select * from onepiece where id={$cid}";
$rs = $this->mysqli->query($sql);
if ($row = $rs->fetch_assoc()) {
$result[] = $row;
$this->getLink($row['pid'], $result);
}
return array_reverse($result);
} /**
* 展现导航Link
* @param [type] $cid [description]
* @return [type] [description]
*/
public function displayLink($cid)
{
$rs = $this->getLink($cid);
$str = '';
foreach ($rs as $val) {
$str .= "<a href=''>{$val['name']}</a>>";
} return $str;
} /**
* 增加分类
* @param [type] $pid 父类id
* @param [type] $name 本类名
*/
public function addNodes($pid, $name)
{
$sql = "insert into onepiece values('',{$pid},'" . $name . "')";
if ($this->mysqli->query($sql)) { return true; }
} /**
* 删除分类
* @param [type] $id 本类id
* @return [type]
*/
public function deleteNodes($id)
{
$sql = "select * from onepiece where pid ={$id}";
$rs = $this->mysqli->query($sql);
if ($row = $rs->fetch_assoc()) {
$mes = "还有子元素,请勿删除";
} else {
$sql = "delete from onepiece where id={$id}";
if ($this->mysqli->query($sql)) {
$mes = "删除成功";
}
}
return $mes;
}
} $config = array('host' => 'localhost', 'db' => 'w', 'user' => 'w', 'pwd' => 'w');
$wr = new Unlimited($config);
echo $wr->displayList();
<?php
/**
* @name PHPTree
* @des PHP生成树形结构,无限多级分类
* @version 2.0.1
* @updated 2016-08-26 */
class PHPTree{ protected static $config = array(
/* 主键 */
'primary_key' => 'id',
/* 父键 */
'parent_key' => 'parent_id',
/* 展开属性 */
'expanded_key' => 'expanded',
/* 叶子节点属性 */
'leaf_key' => 'leaf',
/* 孩子节点属性 */
'children_key' => 'children',
/* 是否展开子节点 */
'expanded' => false
); /* 结果集 */
protected static $result = array(); /* 层次暂存 */
protected static $level = array();
/**
* @name 生成树形结构
* @param array 二维数组
* @return mixed 多维数组
*/
public static function makeTree($data,$options=array() ){
$dataset = self::buildData($data,$options);
$r = self::makeTreeCore(0,$dataset,'normal');
return $r;
} /* 生成线性结构, 便于HTML输出, 参数同上 */
public static function makeTreeForHtml($data,$options=array()){ $dataset = self::buildData($data,$options);
$r = self::makeTreeCore(0,$dataset,'linear');
return $r;
} public static function getResult() {
return self::$result;
} /* 格式化数据, 私有方法 */
private static function buildData($data,$options){
$config = array_merge(self::$config,$options);
self::$config = $config;
extract($config); $r = array();
foreach($data as $item){
$id = $item[$primary_key];
$parent_id = $item[$parent_key];
$r[$parent_id][$id] = $item;
} return $r;
} /* 生成树核心, 私有方法 */
private static function makeTreeCore($index,$data,$type='linear')
{
extract(self::$config);
if(!isset($data[$index])) {
return;
}
foreach($data[$index] as $id=>$item)
{
$itemid = $item[$primary_key];
if($type=='normal'){
self::$result[$itemid] = $item;
if(isset($data[$id]))
{
$item[$expanded_key]= self::$config['expanded'];
$item[$children_key]= self::makeTreeCore($id,$data,$type);
}
else
{
$item[$leaf_key]= true;
}
$r[] = $item;
}else if($type=='linear'){
$parent_id = $item[$parent_key];
self::$level[$id] = $index==0?0:self::$level[$parent_id]+1;
$item['level'] = self::$level[$id];
self::$result[$itemid] = $item;
if(isset($data[$id])){
self::makeTreeCore($id,$data,$type);
} $r = self::$result;
}
}
return $r;
}
} $dbh = new PDO('mysql:host=localhost;dbname=apiamz', "root", "root"); $sql = 'SELECT MAX(PurchaseDate),MIN(PurchaseDate),COUNT(*) FROM listorders';
foreach ($dbh->query($sql) as $row) {
print_r($row);
} $category = PHPTree::makeTree($data,[
'primary_key' => 'cateid',
'parent_key' => 'parentid'
]); die();
DROP TABLE IF EXISTS `menu0910`;
CREATE TABLE `menu0910` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`menu` varchar(50) COLLATE utf8_unicode_ci DEFAULT '',
`parentid` bigint(20) DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- ----------------------------
-- Records of menu0910
-- ----------------------------
INSERT INTO `menu0910` VALUES ('', '1-1', '');
INSERT INTO `menu0910` VALUES ('', '1-2', '');
INSERT INTO `menu0910` VALUES ('', '1-3', '');
INSERT INTO `menu0910` VALUES ('', '1-1-1', '');
INSERT INTO `menu0910` VALUES ('', '1-1-2', '');
INSERT INTO `menu0910` VALUES ('', '1-1-3', '');
INSERT INTO `menu0910` VALUES ('', '1-2-1', '');
INSERT INTO `menu0910` VALUES ('', '1-2-2', '');
INSERT INTO `menu0910` VALUES ('', '1-2-3', '');
INSERT INTO `menu0910` VALUES ('', '1-3-1', '');
INSERT INTO `menu0910` VALUES ('', '1-3-2', '');
INSERT INTO `menu0910` VALUES ('', '1-3-3', '');
INSERT INTO `menu0910` VALUES ('', '1-1-1-1', '');
INSERT INTO `menu0910` VALUES ('', '1-1-1-2', '');
INSERT INTO `menu0910` VALUES ('', '1-1-1-3', '');
INSERT INTO `menu0910` VALUES ('', '1-1-2-1', '');
INSERT INTO `menu0910` VALUES ('', '1-1-2-2', '');
INSERT INTO `menu0910` VALUES ('', '1-1-2-3', '');
INSERT INTO `menu0910` VALUES ('', '1-1-3-1', '');
INSERT INTO `menu0910` VALUES ('', '1-1-3-2', '');
INSERT INTO `menu0910` VALUES ('', '1-1-3-3', '');
INSERT INTO `menu0910` VALUES ('', '1-2-1-1', '');
INSERT INTO `menu0910` VALUES ('', '1-2-1-2', '');
INSERT INTO `menu0910` VALUES ('', '1-2-1-3', '');
INSERT INTO `menu0910` VALUES ('', '1-2-2-1', '');
INSERT INTO `menu0910` VALUES ('', '1-2-2-2', '');
INSERT INTO `menu0910` VALUES ('', '1-2-2-3', '');
INSERT INTO `menu0910` VALUES ('', '1-2-3-1', '');
INSERT INTO `menu0910` VALUES ('', '1-2-3-2', '');
INSERT INTO `menu0910` VALUES ('', '1-2-3-3', '');
INSERT INTO `menu0910` VALUES ('', '1-3-1-1', '');
INSERT INTO `menu0910` VALUES ('', '1-3-1-2', '');
INSERT INTO `menu0910` VALUES ('', '1-3-1-3', '');
INSERT INTO `menu0910` VALUES ('', '1-3-2-1', '');
INSERT INTO `menu0910` VALUES ('', '1-3-2-2', '');
INSERT INTO `menu0910` VALUES ('', '1-3-2-3', '');
INSERT INTO `menu0910` VALUES ('', '1-3-3-1', '');
INSERT INTO `menu0910` VALUES ('', '1-3-3-2', '');
INSERT INTO `menu0910` VALUES ('', '1-3-3-3', '');
<?php

class PHPTree1
{ function __construct($DBPrimaryKeyFieldName, $DBParentFieldName, $MinDataLevelNum = 0)
{
$this->MinDataLevelNum = $MinDataLevelNum;
$this->DBPrimaryKeyFieldName = $DBPrimaryKeyFieldName;
$this->DBParentFieldName = $DBParentFieldName; $this->Result = array();
$this->LevelList = array();
} public function SameLevelData($DBData)
{
$DBPrimaryKeyFieldName = $this->DBPrimaryKeyFieldName;
$DBParentFieldName = $this->DBParentFieldName;
$Arr = array();
foreach ($DBData as $w) {
$PrimaryKeyId = $w[$DBPrimaryKeyFieldName];
$ParentId = $w[$DBParentFieldName];
$Arr[$ParentId][$PrimaryKeyId] = $w;
}
return $Arr;
} public function GetTreeResult($DBData)
{
$MinDataLevelNum = $this->MinDataLevelNum;
$SameLevelDataSet = $this->SameLevelData($DBData);
$Result = $this->MakeTreeRecursion($MinDataLevelNum, $SameLevelDataSet);
return $Result;
} private function MakeTreeRecursion($TreeIndex, $SameLevelDataSet)
{
$MinDataLevelNum = $this->MinDataLevelNum;
$DBPrimaryKeyFieldName = $this->DBPrimaryKeyFieldName;
$DBParentFieldName = $this->DBParentFieldName;
$LevelList = $this->LevelList;
if (!isset($SameLevelDataSet[$TreeIndex])) {
return;
}
foreach ($SameLevelDataSet[$TreeIndex] as $key => $val) {
$ParentId = intval($val[$DBParentFieldName]);
$Level[$ParentId] = $TreeIndex == $MinDataLevelNum ? $MinDataLevelNum : self::$Level[$ParentId] + 1;
$LevelList[$ParentId]
$Val['level'] = self::$level[$id];
$this->LevelSet = $this->DBParentFieldName
self::$Result[$ValId] = $Val;
if (isset($Data[$ValId])) {
self::MakeTreeRecursion($id, $data, $type);
}
$r = self::$result;
}
return $r;
}
} try {
$sql = 'SELECT * FROM menu0910';
$wdata = $dbh->query($sql);
foreach ($wdata as $row) {
print_r($row);
}
} catch (PDOException $w) {
echo $w;
} $category = PHPTree1::makeTree($wdata); var_dump($category); die(); /**
* @name PHPTree
* @des PHP生成树形结构,无限多级分类
* @version 2.0.1
* @updated 2016-08-26
*/
class PHPTree
{ protected static $config = array(
/* 主键 */
'primary_key' => 'id',
/* 父键 */
'parent_key' => 'parentid',
/* 展开属性 */
'expanded_key' => 'expanded',
/* 叶子节点属性 */
'leaf_key' => 'leaf',
/* 孩子节点属性 */
'children_key' => 'children',
/* 是否展开子节点 */
'expanded' => false
); /* 结果集 */
protected static $result = array(); /* 层次暂存 */
protected static $level = array(); /**
* @name 生成树形结构
* @param array 二维数组
* @return mixed 多维数组
*/
public static function makeTree($data, $options = array())
{
$dataset = self::buildData($data, $options);
$r = self::makeTreeCore(0, $dataset, 'normal');
return $r;
} /* 生成线性结构, 便于HTML输出, 参数同上 */
public static function makeTreeForHtml($data, $options = array())
{ $dataset = self::buildData($data, $options);
$r = self::makeTreeCore(0, $dataset, 'linear');
return $r;
} public static function getResult()
{
return self::$result;
} /* 格式化数据, 私有方法 */
private static function buildData($data, $options)
{
$config = array_merge(self::$config, $options);
self::$config = $config;
extract($config); $r = array();
foreach ($data as $item) {
$id = $item[$primary_key];
$parent_id = $item[$parent_key];
$r[$parent_id][$id] = $item;
} return $r;
} /* 生成树核心, 私有方法 */
private static function makeTreeCore($index, $data, $type = 'linear')
{
extract(self::$config);
if (!isset($data[$index])) {
return;
}
foreach ($data[$index] as $id => $item) {
$itemid = $item[$primary_key];
if ($type == 'normal') {
self::$result[$itemid] = $item;
if (isset($data[$id])) {
$item[$expanded_key] = self::$config['expanded'];
$item[$children_key] = self::makeTreeCore($id, $data, $type);
} else {
$item[$leaf_key] = true;
}
$r[] = $item;
} else if ($type == 'linear') {
$parent_id = $item[$parent_key];
//parent_id 0,1,2,3,4,......
self::$level[$id] = $index == 0 ? 0 : self::$level[$parent_id] + 1;
$item['level'] = self::$level[$id];
self::$result[$itemid] = $item;
if (isset($data[$id])) {
self::makeTreeCore($id, $data, $type);
} $r = self::$result;
}
}
return $r;
}
} $dbh = new PDO('mysql:host=localhost;dbname=w', 'root', ''); try {
$sql = 'SELECT * FROM menu0910';
$wdata = $dbh->query($sql);
foreach ($wdata as $row) {
print_r($row);
}
} catch (PDOException $w) {
echo $w;
} $category = PHPTree::makeTree($wdata); var_dump($category); die(); die();

不遗留问题-menu数据拼装-2的更多相关文章

  1. 不遗留问题-menu数据拼装

    DROP TABLE IF EXISTS `menu0910`; CREATE TABLE `menu0910` ( `id` ) NOT NULL AUTO_INCREMENT, `menu` ) ...

  2. 小程序开发笔记【二】,抽奖结果json数据拼装bug解决

    抽奖结果数据json格式数据拼接bug,如下图,只发布了两个奖项,每个奖项设置2个奖品,但最后拼接数据的时候出现3个奖项 json数据格式如下 "luckyResult":[ { ...

  3. MySQL将查询出来的一组数据拼装成一个字符串

    1 前言 由于项目中有一个需求,需要把当日当周的排行榜数据归档,以便后期查询和发放奖励.然而发现,mysql的变量只能存一个变量值,然后如果要储存一条记录,可以使用CONCAT_WS,如果要储存多条记 ...

  4. mysql将查询出来的一列数据拼装成一个字符串

    使用GROUP_CONCAT函数. SELECT GROUP_CONCAT(查询的字段 separator ',') FROM table

  5. 将相关数据拼成所需JSON数据

    参考: http://www.cnblogs.com/shuilangyizu/p/6019561.html 有时候我们需要将一些数据拼装成所需要格式的JSON,可以使用如下方法,本人觉得还是比较方便 ...

  6. poi导出excel,表头数据动态拼装

    /* * 第一步:拼装表头和数据 */ // 放多个sheet的集合 List<Map<String,Object>> datas = new ArrayList<Map ...

  7. java递归算法实现拼装树形JSON数据

    有时候页面需要使用jQuery easy ui中的combotree,需要给combotree提供一个JSON数据,使用如下方法(递归)实现(下面是dao层的实现层): /** * 根据表名和父id拼 ...

  8. 【Raspberry Pi】新入手的Raspberry Pi3拼装日志

    一.概述 2016年暑假某宝入手Raspberry Pi 3,装机清单: 树莓派主板 亚克力外壳 小风扇 散热片 30G SD card 螺丝若干颗 因机型问题,可能与你的机器有微小差异 二.装机过程 ...

  9. Ibatis动态拼装sql,常用标签总结及举栗子。

    今天得到项目经理一项任务,就是拼装sql,第一次见到,不是太懂,赶紧回来睡一觉再说,由于这次的项目orm使用的是ibatis框架,所以需要使用动态拼装sql,或者是ognl语言,这门语言不是专属于ib ...

随机推荐

  1. 【软件工程】week5-个人作业-敏捷开发方法初窥

    敏捷开发方法初窥 引言:本周的软件工程个人博客作业是阅读关于敏捷开发方法的文章(http://martinfowler.com/agile.html),并撰写自己的读后感.文章内容非常丰富,对敏捷开发 ...

  2. 利用mapreduce将数据从hdfs导入到hbase遇到的问题

    现象: 15/08/12 10:19:30 INFO mapreduce.Job: Job job_1439396788627_0005 failed with state FAILED due to ...

  3. ubuntu下新建用户的终端不显示当前路径,不能用上下光标键得到使用过的命名解决办法

    这几天我装ubuntu10.10,xubuntu12.04创建新用户的时候,总会遇到这个问题 就是打开终端的时候,没有路径了,即:xxx@xxx:~$ 找了很久,最后找到了(http://www.os ...

  4. COJ983 WZJ的数据结构(负十七)

    显然是动态树裸题:O(mlogn) #include<cstdio> #include<cstring> #include<algorithm> #include& ...

  5. Mysql_mysql 性能分析及explain用法

    1 使用explain语句去查看分析结果,如  explain select * from test1 where id=1;会出现:id  selecttype  table  type possi ...

  6. oracle中用SQL实现两个日期间的日期形成一个数据集

    比如输入2014-06-1 和 2014-07-1形成一个2014-06-1 2014-06-22014-06-3...2014-07-1 的数据集.   解决方法: select date'2014 ...

  7. C#引用Interop.SQLDMO.dll后的注意事项(转)

    C#引用sqldmo.dll的方法 找到 sqldmo.dll这个文件C:\Program Files\Microsoft SQL Server\80\Tools\Binn\sqldmo.dll用.N ...

  8. OFFICE 修改记录保存在单元格批注中vba

    Dim ydtext As String '原单元格值 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Cells.Coun ...

  9. Java 利用Apache Commons Net 实现 FTP文件上传下载

    package woxingwosu; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...

  10. 关于ztree异步加载的问题(二)

    本来以为这个异步加载会很难控制,因为考虑到ztree节点图标的控制,结果并不是那么困难,ztree自己控制图标,你只要在json中设置isParent:true,它自己会识别父节点并控制图标,以下是核 ...