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. Fiddler Web Debugger Tool

    The Fiddler tool helps you debug web applications by capturing network traffic between the Internet ...

  2. 在HTML中优雅的生成PDF

    关键代码<script type=</text>          </para>          <para align=" value=" ...

  3. vijos p1002 dp ***

    链接:点我 一开始的方程式很好写的,但是数据太大,考虑到石子数目很小,我们对其进行离散化,若相邻两点距离超过ya的值,则后面所有点都往前移动,这里ya的值是可以调整的 #include <ios ...

  4. hdu 1114 基础完全背包

    题意:给一个储钱罐,已知空的储钱罐和装了硬币的储钱罐的质量.然后给了n种硬币的质量和价值. 问储钱罐里最少有多少钱. 解法:完全背包.注意要初始化为 INF,要正好装满,如果结果是INF,输出This ...

  5. img 默认是行内元素,它旁边的空格是会保留的

    img 默认是行内元素,它旁边的空格是会保留的.因此图片宽度设置百分百后下面会有点找不出原因的间隙. 解决办法:img{display:block}

  6. 二叉索引树BIT

    定义     二叉索引树,binary index tree,又名树状数组,或Fenwick Tree,因为本算法由Fenwick创造.     对于数组A,定义Query(i,j) = Ai +Ai ...

  7. ISODATA算法

    ISODATA算法是在k-均值算法的基础上,增加对聚类结果的'合并'和'分裂'两个操作,并 设定算法运行控制参数的一种聚类算法. 全称:Iterative Selforganizing Data An ...

  8. 转盘游戏[XDU1006]

    Problem 1006 - 转盘游戏 Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty: Total Submit: 87  Accep ...

  9. javascript继承扩展类方法实现

    javascript没有原生的继承语法,这确实很让人困惑,但是广大人民群从的智慧是无穷的.最近呢,正尝到一点从源码中学习的甜头,不分享一下实在难以平复激动的心情.前不久改造视频播放插件的时候,找到了v ...

  10. 转:移动端html页面优化

    对于访问量大的网站来说,前端的优化是必须的,即使是优化1KB的大小对其影响也很大,下面来看看来自ISUX的米随随讲讲移动手机平台的HTML5前端优化,或许对你有帮助和启发. 概述 1. PC优化手段在 ...