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

问题描述 投票:0回答:3

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

表格数据:

| 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

    //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

//等等....

}

看到第二个循环的 child ID 的值为

3.1.1

在第二个循环之后,第二个循环的 child ID 必须在第三个循环中用作 parent 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的图表布局结果:

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

javascript php json dhtmlx
3个回答
2
投票
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);

1
投票

我解决了我的问题,这是我对我的问题的回答:

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

];

结果:


0
投票

请先生提供文件,以便我测试一次

© www.soinside.com 2019 - 2024. All rights reserved.