PHP生成tree需要的Json数据,数据来自于MySQL
2010-12-19 23:35
330 查看
似乎是有问题的代码,思路供参考:
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;
?>
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+MySQL+Easyui tree菜单从后台加载json数据(一)
- PHP+Mysql+easyui点击左侧tree菜单对应表名右侧动态生成datagrid加载表单数据(二)
- 生成符合EasyUI-Tree的JSON数据
- PHP使用json_encode生成json数据,使用json_decode对JSON解码
- PHP生成json数据
- C#将datatable生成easyui的绑定tree 的json数据格式
- php将mysql数据表(含中文数据)导出生成excel表,快速生成且解决中文乱码问题。
- PHP mysql_fetch_array遍历数据时的问题(指针需要复位)
- LigerUI中生成GRID与TREE的【JSON数据】与Asp.Net的【变量数据】(方便测试)
- PHP读取MySQL生成XML格式数据
- 用PHP生成mysql数据字典
- C#将datatable生成easyui的绑定tree 的json数据格式
- php生成mysql的数据字典
- Android 连接Mysql进行相关操作——用PHP做中介以及JSON做数据交换
- PHP 从数据库Mysql中读取数据生成excel(解决乱码问题,解决中文变问号问题)
- C#将datatable生成easyui的绑定tree 的json数据格式
- php查询mysql,返回json格式数据
- zTree的联想[将数据表封装成自定义TreeNode,再将TreeNode生成json字串
- php 生成json格式的数据
- android 通过json格式提交数据给php 调用数据库mysql