gpt4 book ai didi

javascript - 使用 PHP 和 Json 的动态 dhtmlx 组织结构图

转载 作者:行者123 更新时间:2023-12-01 02:06:52 25 4
gpt4 key购买 nike

我有一个问题。我真的不明白应该如何使用循环正确输出它。

表格数据:

| Name               |        Position | 
|:-------------------|----------------:|
| MARK NICHOLS | Team Lead|
| NICHOLAS CRUZ | Team Lead|
| SEAN PARKER | Programmer|
| MICHAEL SHAW | Programmer|
| LAURA ALVAREZ | Junior|
| JOHN FLORES | Junior|
<小时/>

我想要的是,在每 2 个获取的数据中,json 中的 ID 和父变量必须像这样更改:

while loop of table from above(){

在这部分,这是 2 个数据的第一个循环中的默认值

3.1 is a CHILD ID and 3.2

3 is a PARENT ID and 3

    //1loop            
3.1 3.2
3 3

第一次循环后的动态循环

   //2loop
CHILD 3.1.1 3.2.1
PARENT 3.1 3.2
//3loop
CHILD 3.1.1.1 3.2.1.1
PARENT 3.1.1 3.2.1
//4loop
CHILD 3.1.1.1.1 3.2.1.1.1
PARENT 3.1.1.1 3.2.1.1

//等等......

}

看到第二个循环的子ID值为3.1.1

在第二个循环之后,第二个循环的子 ID 必须在第三个循环中用作父 ID

假设在子进程的每个循环中添加 .1 。父循环是最后一个子循环。

静态 Json:

 {
"id": "3.1",
"text": "Team Lead",
"title": "MARK NICHOLS",
"img": "../common/img/avatar-10.png",
"parent": "3"
},
{
"id": "3.2",
"text": "Team Lead",
"title": "NICHOLAS CRUZ",
"img": "../common/img/avatar-10.png",
"parent": "3"
},
{
"id": "3.1.1",
"text": "Programmer",
"title": "SEAN PARKER",
"img": "../common/img/avatar-10.png",
"parent": "3.1"
},
{
"id": "3.2.1",
"text": "Programmer",
"title": "MICHAEL SHAW",
"img": "../common/img/avatar-8.png",
"parent": "3.2"
},{
"id": "3.1.1.1",
"text": "Junior",
"title": "LAURA ALVAREZ",
"img": "../common/img/avatar-10.png",
"parent": "3.1.1"
},
{
"id": "3.2.1.1",
"text": "Junior",
"title": "JOHN FLORES",
"img": "../common/img/avatar-8.png",
"parent": "3.2.1"
}

静态Json的图表布局结果:

enter image description here

如果我使用上面的表数据并使用 SQL 选择所有数据会怎样。这个 PHP 代码必须像上面的静态 Json 一样转换吗?

我的试用代码:

<?php 
$count = 6;
$a = "3.";
$b = "3";
$c = 1;
$e = "";
$f = "";
echo "<pre>";
for ($i=1; $i <=$count; $i++){
if ($c == 1){
$d =1;
}
if ($c == 2){
$d = 2;
}
echo "{";
echo "ID:".$a.$c.$e.$f.",";



$parent = $a.$c.$e.$f;


echo "Parent:".$parent;
echo "}";
if($i == $count){
}
else{
echo ",";
}

if ($c == 2){

$c = 0;

$e =".1";
}
$f += $e;
$c++;
}

?>

逻辑错误结果:

{ID:3.1,Parent:3.1},
{ID:3.20,Parent:3.20},
{ID:3.1.10.1,Parent:3.1.10.1},
{ID:3.2.10.2,Parent:3.2.10.2},
{ID:3.1.10.3,Parent:3.1.10.3},
{ID:3.2.10.4,Parent:3.2.10.4}

我的试用代码的预期输出:

{ID:3.1,Parent:3},
{ID:3.2,Parent:3},
{ID:3.1.1,Parent:3.1},
{ID:3.2.1,Parent:3.2},
{ID:3.1.1.1,Parent:3.1.1},
{ID:3.2.1.1,Parent:3.2.1}

如果有人对 Json 和 PHP 有丰富的知识,请帮助我使用循环将动态 dhtmlx 图表输出到 Json。其他方法也将被接受。

最佳答案

function loop(array $parents, $need)
{
$children = [];
$isLast = $need === 1;
$lastKey = count($parents) - 1;
foreach ($parents as $key => $parent) {
$id = $parent === 3 ? $key + 1 : 1;
$children[] = $child = "$parent.$id";
$comma = $isLast && $key === $lastKey ? '' : ',';
echo "{ID:$child,Parent:$parent}$comma" . "<br/>";
}

$need--;

if ($need) {
return loop($children, $need);
}

return $children;
}


loop([3, 3], 4);

关于javascript - 使用 PHP 和 Json 的动态 dhtmlx 组织结构图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50028788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com