Skip to content Skip to sidebar Skip to footer

Dynamic Dhtmlx Organization Chart Using Php And Json

i have a problem. i don't really understand how should i output this correctly using loops. Table Data: | Name | Position | |:-------------------|------------

Solution 1:

functionloop(array$parents, $need)
{
    $children = [];
    $isLast = $need === 1;
    $lastKey = count($parents) - 1;
    foreach ($parentsas$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);

Solution 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 ($parentsas$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:enter image description here

Post a Comment for "Dynamic Dhtmlx Organization Chart Using Php And Json"