使用PHP和Json

时间:2018-04-25 18:09:46

标签: javascript php json dhtmlx

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

表格数据

| 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是儿童身份证,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

//等等....

}

查看第二个循环的子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 Above?

我的试用代码:

<?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。 其他方法将被接受。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

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

<强> SQL:

0

统计查询内容:

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

然后我改进qskane回答:

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

  }

}

<强>结果: enter image description here