您的位置:首页 > 编程语言 > PHP开发

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;

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: