不遗留问题-menu数据拼装-2
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('  ', $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的更多相关文章
- 不遗留问题-menu数据拼装
DROP TABLE IF EXISTS `menu0910`; CREATE TABLE `menu0910` ( `id` ) NOT NULL AUTO_INCREMENT, `menu` ) ...
- 小程序开发笔记【二】,抽奖结果json数据拼装bug解决
抽奖结果数据json格式数据拼接bug,如下图,只发布了两个奖项,每个奖项设置2个奖品,但最后拼接数据的时候出现3个奖项 json数据格式如下 "luckyResult":[ { ...
- MySQL将查询出来的一组数据拼装成一个字符串
1 前言 由于项目中有一个需求,需要把当日当周的排行榜数据归档,以便后期查询和发放奖励.然而发现,mysql的变量只能存一个变量值,然后如果要储存一条记录,可以使用CONCAT_WS,如果要储存多条记 ...
- mysql将查询出来的一列数据拼装成一个字符串
使用GROUP_CONCAT函数. SELECT GROUP_CONCAT(查询的字段 separator ',') FROM table
- 将相关数据拼成所需JSON数据
参考: http://www.cnblogs.com/shuilangyizu/p/6019561.html 有时候我们需要将一些数据拼装成所需要格式的JSON,可以使用如下方法,本人觉得还是比较方便 ...
- poi导出excel,表头数据动态拼装
/* * 第一步:拼装表头和数据 */ // 放多个sheet的集合 List<Map<String,Object>> datas = new ArrayList<Map ...
- java递归算法实现拼装树形JSON数据
有时候页面需要使用jQuery easy ui中的combotree,需要给combotree提供一个JSON数据,使用如下方法(递归)实现(下面是dao层的实现层): /** * 根据表名和父id拼 ...
- 【Raspberry Pi】新入手的Raspberry Pi3拼装日志
一.概述 2016年暑假某宝入手Raspberry Pi 3,装机清单: 树莓派主板 亚克力外壳 小风扇 散热片 30G SD card 螺丝若干颗 因机型问题,可能与你的机器有微小差异 二.装机过程 ...
- Ibatis动态拼装sql,常用标签总结及举栗子。
今天得到项目经理一项任务,就是拼装sql,第一次见到,不是太懂,赶紧回来睡一觉再说,由于这次的项目orm使用的是ibatis框架,所以需要使用动态拼装sql,或者是ognl语言,这门语言不是专属于ib ...
随机推荐
- Python实现的粒子群优化算法
01.from numpy import array 02.from random import random 03.from math import sin, sqrt 04. 05.iter_ma ...
- Android 下载文件 显示进度条
加入两个权限 一个是联网,另一个是读写SD卡 <uses-permission android:name="android.permission.INTERNET">& ...
- DP ZOJ 3872 Beauty of Array
题目传送门 /* DP:dp 表示当前输入的x前的包含x的子序列的和, 求和方法是找到之前出现x的位置(a[x])的区间内的子序列: sum 表示当前输入x前的所有和: a[x] 表示id: 详细解释 ...
- LightOJ1079 Just another Robbery(DP)
题目大概是抢劫银行,每家银行都有一个收益和一个被抓的概率,求在被抓概率小于P的条件下能获得的最大收益. dp[i][j]表示抢劫前i家银行收益为j被抓的最小概率 dp[0][0]=0 dp[i][j] ...
- silverlight 跨域访问 wcf
先介绍一下我的测试项目,我用flash和silverlight一同来调用一个webservice,一个flash客户端,一个silverlight客户端,一个web项目来host flash和silv ...
- 【POJ】1279 Art Gallery
http://poj.org/problem?id=1279 题意:给一个n个点的多边形,n<=1500,求在多边形内能看到所有多边形上的点的面积. #include <cstdio> ...
- ASCII Table - ASCII码对照表
ASCII控制字符 二进制 十进制 十六进制 缩写 可以显示的表示法 名称/意义 0000 0000 0 00 NUL ␀ 空字符(Null) 0000 0001 1 01 SOH ␁ 标题开始 00 ...
- QRadioButton 使用方法
QRadioButton 控件是Qt中实现多选一功能的控件,它的使用方法如下: 声明控件: QRadioButton *rbutton; 然后实现它的响应函数: void YourClass::on_ ...
- Web移动端设计——移动设备分辨率一览表
作为在移动端开发的web程序员来说,如果不懂设备的一些性能,在开发上面是非常耗时间的一件事,同时带来负面影响的是项目的进度被拖腿了. 下面是个人收集的一些移动端设备的分辨率参数: 1. 平板设备: ...
- 对于flat_interface与public_interface的理解
对于这两个interface含义的理解一波三折,下面我把各种理解都记录下来. 2014-9-23 #可以把flat_interface理解为Openstack整套生态系统内部的网络接口,内部各 ...