PHP生成tree需要的Json数据,数据来自于MySQL
来源:互联网 发布:孕期软件下载 编辑:程序博客网 时间:2024/06/08 08:23
似乎是有问题的代码,思路供参考:
The general format for extjs tree JSON is as below...
[{text:'A',
icon :'/new_cms/cms/ext/resources/images/accord/page_green.png',
qtip:'test',id:'1',
children:[
{text:'A_1',
icon:'/new_cms/cms/ext/resources/images/accord/page_green.png',
id:'3',
qtip:'demo',allowDelete:true,children:[]},]},]
+++++++++++++++++++++
i genrated this using following code...
This code works fine only for first level
if i insert a child to a the tree breaks up
pls help me with the correct logic
or give me the link where i can find help
++++++++++++++++++++++++++++++
The array variable $menu_name should be the text: in JSON
the tree_id will be the id: in JSON
$tree_id[] is a reference like primary key
$menu_id[] decides the tree level
if the $menu_id is 1 or 2 or 3 ..... so on without any _(underscore)
its the first level..
if $menu_id is 1_1 then its the child of 1
if $menu_id is 1_2 then its the child of 1
if $menu_id is 1_1_1 then its the child of 1_1
feel free to ask any info about the coding and other issues
===============================================
<?
$new_sub="[";
$new_sub_end="]";
$tree_content="";
$tree_array=array();
$root_node=array();
$array_count=0;
$root_count=0;
$con= mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname",$con);
$root = mysql_query("SELECT * FROM menu_managment_table where
menu_category='primary' ORDER BY tree_id ASC ");
$tree_content.=$new_sub;
$m=1;
while($row = mysql_fetch_assoc($root))
{
$menu_name[$m]=$row['menu_name'] ;
$menu_id[$m]=$row['tree_id'];
$tree_id[$m]=$row['menu_id'];
$m++;
}
$limit=1;
$itration=0;
for($i=1;$i<=count($menu_id);$i++ )
{
$next=1;
$str=$i;
$var= array_search($str,$menu_id);
if(!empty($var))
{
$tpm= explode('_',$str);
$tpm_limit=count($tpm);
//echo $tpm_limit."<br>";
while($tpm_limit<=$limit)
{
$sub=$str."_".$next;
$sub_var=array_search($sub,$menu_id);
if(!empty($sub_var))
{
$qtip = 'Last Modified: '.
$lastmod.'<br />Size: '.$size;
$tree_array[++
$array_count]="{text:'".$menu_name[$var]."',icon :'/
new_cms/cms/ext/resources/images/accord/
page_green.png',qtip:'test',id:'".$tree_id[$var]."',children:[";
$var=$sub_var;
$root_node[++$root_count]=
$str;
++$itration;
$str=$sub;
}else
{
$tree_array[++
$array_count]="{text:'".$menu_name[$var]."',icon:'/
new_cms/cms/ext/resources/images/accord/page_green.png',id:'".
$tree_id[$var]."',qtip:'demo',allowDelete:true,children:[]},";
$var1=rep($sub );
if($var1)
{}else{break;}
}
}
}
}
function rep($sub )
{
//echo $sub."<br>";
global $tpm,$tpm_limit,$str,$menu_id,$var;
$tpm=explode("_",$sub);
$tpm_limit=count($tpm);
if($tpm_limit>2)
{
unset($tpm[$tpm_limit-1]);
++$tpm[count($tpm)-1];
$str=join("_",$tpm);
$var= array_search($str,$menu_id);
if(empty($var))
{
if( rep($str ))
{
return true;
}
}else{
return true;
}
}else
{
return false;
}
}
for($j=1;$j<=count($root_node);$j++)
{
$key=0;
for($i=1;$i<=count($menu_id);$i++)
{
if (strncasecmp($root_node[$j],
$menu_id[$i],strlen($root_node[$j]))==0){
$key=$i;
}
}
if($key){
$tree_array[$key]=$tree_array[$key]."]},";
}
}
//var_dump($tree_array);
$tree_content= $new_sub.join('',$tree_array).$new_sub_end ;
echo $tree_content;
?>
- PHP生成tree需要的Json数据,数据来自于MySQL
- 生成符合EasyUI-Tree的JSON数据
- PHP生成json数据
- JSON数据的生成
- JSON数据的生成
- 生成MySQL数据字典的PHP脚本
- 对Json数据的处理(模拟获取数据是来自于本地)
- PHP 生成Mysql数据字典
- php生成mysql数据字典
- php生成mysql数据字典
- php生成mysql数据字典
- Json 数据的解析 ,生成
- mysql 收集状态和性能数据的脚本来自于高性能mysql
- php将json格式的数据直接存入mysql数据库
- php将json格式的数据直接存入mysql数据库
- 【JSON】生成JSON数据的方式
- 【JSON】生成JSON数据的方式
- MySQL数据读取,ExtJS和PHP Json
- Tomcat启动
- 智能卡数据传输协议
- USB入门系列之五 ---- USB设备的枚举过程(zz)
- 12月19日记
- 视频格式介绍
- PHP生成tree需要的Json数据,数据来自于MySQL
- 第一个PHP网页
- Delphi输出日志的方法
- 函数指针的两个例子,
- Molehill 3D APIs - GPU硬件加速的Flash 3D API
- FOJ 1573 大学自习室
- 由preempt_disable的实现想到的
- Android 2.3编译不过的问题
- 取USB设备信息