Dynamic dhtmlx organization Chart using PHP and Json

独自空忆成欢 提交于 2020-01-06 05:38:08

问题


i have a problem. i don't really understand how should i output this correctly using loops.

Table Data:

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

I want is in every 2 fetched data the ID and Parent Variable in json must be change like this.:

while loop of table from above(){

on this part this is the default value in first loop of 2 data

3.1 is a CHILD ID and 3.2

3 is a PARENT ID and 3

    //1loop            
                3.1             3.2
                3               3

dynamic loop after the first loop

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

//and so on....

}

see the child ID of second loop has a value of 3.1.1

after that second loop the child ID of second loop must be use in 3rd loop as a parent ID

let say in every loop of child add .1 . and the parent is the last child loop.

Static 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"
  }

Chart Layout Result Of Static Json:

What if i use the Table Data Above And Select All Data Using SQL. this PHP code must be convert like the Static Json Above?

My Trial Code:

<?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++;
}

    ?>

Logical Error Result:

{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}

Expected output on my trial code:

{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}

Please Help Me, If Someone There Having A Great Knowledge On Json and PHP to output Dynamic dhtmlx chart using loop to Json. Other Method Will Be Accepted.


回答1:


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



回答2:


i fix my problem here's my answer on my question:

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 the query content:

$count_official = mysqli_num_rows($sql);

Declaring temp array:

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

Fetch the data and save to 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";

  }

}

and then i improve qskane answer:

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

];

Result:



来源:https://stackoverflow.com/questions/50028788/dynamic-dhtmlx-organization-chart-using-php-and-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!