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

lrl1mhuk  于 2023-09-29  发布在  PHP
关注(0)|答案(2)|浏览(117)

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

表格数据:

| 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是孩子ID,3.2
3是父ID,3是父ID

//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
假设在child的每个循环中添加.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的图表布局结果:

如果我使用上面的表数据并使用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.其他方法将被接受。

qaxu7uf2

qaxu7uf21#

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);
bjp0bcyl

bjp0bcyl2#

我解决了我问题以下是我对我问题的回答:

SQL:

$sql = mysqli_query($conn,"SELECT * FROM `brgy_official_detail` bod 
INNER JOIN resident_detail rd ON rd.res_ID = bod.res_ID
LEFT JOIN ref_suffixname rs ON rs.suffix_ID = rd.suffix_ID
LEFT JOIN ref_position rp ON rp.position_ID = bod.commitee_assignID
WHERE visibility = 1 AND  position_Name LIKE 'Barangay Official%'");

统计查询内容:

$count_official = mysqli_num_rows($sql);

声明临时数组:

$name = array();
$position_Name = array();
$official_img = array();

取数据保存到数组:

while($official_data = mysqli_fetch_array($sql)){
  $suffix = $official_data['suffix'];
   if ($suffix == "N/A") {
     $suffix = "";
   }
   else{
      $suffix = $official_data['suffix'];
   }
  $name[] =  $official_data['res_fName'].' '.$official_data['res_mName'].' '.$official_data['res_lName'].' '.$suffix;
  $position_Name[] = $official_data['position_Name'];

  if (isset($official_data['res_Img'])) {
    $z  = $official_data['res_Img'];
    $official_img[] = "data:image/jpeg;base64,".base64_encode($z);

  } 
  else{
    $official_img[] = "../../Img/Icon/logo.png";

  }

}

然后我改进了qskane答案:

function loop(array $parents, $need,$index,$name,$position_Name,$official_img)
{

    $children = [];
    $isLast = $need === 1;
    $lastKey = count($parents) - 1;

    foreach ($parents as $key => $parent) {
        $p_name = $name[$index];
        $pos_Name = $position_Name[$index];
        $img = $official_img[$index];
        $id = $parent === 3 ? $key + 1 : 1;
        $children[] = $child = "$parent.$id";
        $comma = $isLast && $key === $lastKey ? '' : ',';
        echo "{
        \"id\":\"$child\",
        \"text\": \"$pos_Name\",
        \"title\": \"$p_name\",
        \"width\": 350,
        \"img\": \"$img\",
        \"parent\":\"$parent\"
         }$comma";
    }
    $index++;
    $need--;

    if ($need) {
        return loop($children, $need,$index,$name,$position_Name,$official_img);
    }

    return $children;
}

$index = 0;
loop([3], $count_official,$index,$name,$position_Name,$official_img);
  ?>

];

结果:

相关问题