Could not create dynamic table as per json array value using PHP

时间:2018-07-24 10:16:52

标签: php html-table

I am trying to append the dynamic value into table as per json array using PHP but unable to make it into proper format. I am explaining my code below.

static table:

<table width="60%" cellspacing="0" border="1">
      <thead>
        <tr>
          <th>Zone</th>
          <th>Centre</th>
          <th>Institute</th>
        </tr>
      </thead>
      <tbody>
        <tr>
           <td rowspan="5">West Zone</td>
           <td rowspan="3">BBSR</td>
           <td>Institute1</td>
        </tr>
        <tr>
           <td>Institute2</td>
        </tr>
        <tr>
           <td>Institute3</td>
        </tr>
        <tr>
           <td rowspan="2">Khordha</td>
           <td>Institute4</td>
        </tr>
        <tr>
           <td>Institute5</td>
        </tr>
        <tr>
           <td rowspan="3">East zone</td>
           <td rowspan="2">Cuttack</td>
           <td>Institute6</td>
        </tr>
        <tr>
           <td>Institute7</td>
        </tr>
        <tr>
           <td>Khordha</td>
           <td>Institute8</td>
        </tr>
        <tr>
           <td rowspan="2">North Zone</td>
           <td>Puri</td>
           <td>Institute9</td>
        </tr>
        <tr>
           <td>Balasore</td>
           <td>Institute10</td>
        </tr>
      </tbody>
    </table>

This is my table which has static data and here I need to make it dynamic. I am explaining php code below.

table.php:

<?php
$subcen=array(array("Institute"=>"Institute1"),array("Institute"=>"Institute2"),array("Institute"=>"Institute3"));
$cenArr[]=array("center"=>"BBSR","subdata"=>$subcen);
$subcen=array(array("Institute"=>"Institute4"),array("Institute"=>"Institute5"));
$cenArr[]=array("center"=>"Khordha","subdata"=>$subcen);
$resultArr[]=array("zone"=>"West Zone","centerData"=>$cenArr);
$cenArr=array();
$subcen=array(array("Institute"=>"Institute6"),array("Institute"=>"Institute7"));
$cenArr[]=array("center"=>"Cuttack","subdata"=>$subcen);
$subcen=array(array("Institute"=>"Institute8"));
$cenArr[]=array("center"=>"Khordha","subdata"=>$subcen);
$resultArr[]=array("zone"=>"East zone","centerData"=>$cenArr);
$cenArr=array();
$subcen=array(array("Institute"=>"Institute9"));
$cenArr[]=array("center"=>"Puri","subdata"=>$subcen);
$subcen=array(array("Institute"=>"Institute10"));
$cenArr[]=array("center"=>"Balasore","subdata"=>$subcen);
$resultArr[]=array("zone"=>"North Zone","centerData"=>$cenArr);
//echo json_encode($resultArr);
$html='';
$html.='<table width="60%" cellspacing="0" border="1"><thead><tr><th>Zone</th><th>Centre</th><th>Institute</th></tr></thead><tbody>';
foreach ($resultArr as $value) {
    $zonecnt=0;
    foreach ($value['centerData'] as $v) {
        $centercnt=0;
        foreach ($v['subdata'] as $val) {
            $centercnt=$centercnt+count($val);
            $zonecnt=$zonecnt+count($val);
        }
        //echo $centercnt.'</br>';
    }
    //echo $zonecnt.'</br>';
}

?>

Here my requirement is I need to append these array values into that table and result should come with proper original table format.

1 个答案:

答案 0 :(得分:0)

<?php
$resultArr = 
[
  [
    "title"=>"West Zone",
    "children"=>
    [
      [
        "title"=>"BBSR",
        "children"=>      
        [
          ["title"=>"Institute1"],
          ["title"=>"Institute2"],
          ["title"=>"Institute3"]
        ]
      ],
      [
        "title"=>"Khordha",
        "children"=>
        [
          ["title"=>"Institute4"],
          ["title"=>"Institute5"]
        ]
      ]
    ]
  ],
  [
    "title"=>"East zone",
    "children"=>
    [
      [
        "title"=>"Cuttack",
        "children"=>
        [
          ["title"=>"Institute6"],
          ["title"=>"Institute7"]
        ]
      ],
      [
        "title"=>"Khordha",
        "children"=>
        [
          ["title"=>"Institute8"]
        ]
      ]
    ]
  ],
  [
    "title"=>"North Zone",
    "children"=>
    [
      [
        "title"=>"Puri",
        "children"=>
        [
          ["title"=>"Institute9"]
        ]
      ],
      [
        "title"=>"Balasore",
        "children"=>
        [
          ["title"=>"Institute10"]
        ]
      ]
    ]
  ]
];

$html = '<table width="60%" cellspacing="0" border="1">
  <thead>
    <tr>
      <th>Zone</th>
      <th>Centre</th>
      <th>Institute</th>
    </tr>
  </thead>
  <tbody>';
$table = buildTable($resultArr,0);
$html.= $table[0];
$html.= '</tbody></table>';
echo $html;

function buildTable($tbl,$level)
{
  $cnt = 0;
  $res = '';
  foreach ($tbl as $idx => &$row) 
  {
    if($level==0 OR $idx>0) $res.= '<tr>';
    if(isset($row['children']) AND is_array($row['children'])) 
    {
      $child = buildTable($row['children'],$level+1);
      $res.= '<td rowspan="'.$child[1].'">'.$row['title'].'</td>'.$child[0];
      $cnt+= $child[1];
    }
    else
    {
      $res.= '<td>'.$row['title'].'</td></tr>';
      $cnt++;
    }
  }
  return Array($res,$cnt);
}

?>