简体   繁体   中英

Dynamic Html table generate

I have an array like the following:

array(2) { 
     [0]=> string(15) "University Core" 
     [1]=> array(4) { 
          [0]=> array(3) { 
                  [1]=> string(8) "Language" 
                  [3]=> string(2) "12" 
                  [2]=> array(5) { [0]=> string(6) "ENG102" [1]=> string(6) "ENG111" } 
                  } 
          [1]=> array(3) { 
                  [1]=> string(10) "Humanities" 
                  [3]=> string(1) "9" 
                  [2]=> array(4) { [0]=> string(6) "PHI104"  } } 

          [2]=> array(3) { 
                  [1]=> string(15) "Social Sciences" 
                  [3]=> string(1) "9" 
                  [2]=> array(8) { [0]=> string(6) "ECO101" [1]=> string(6) "ECO104" } } 

          [3]=> array(3) { 
                  [1]=> string(7) "Science" 
                  [3]=> string(1) "4" 
                  [2]=> array(2) { [0]=> string(6) "BIO103" [1]=> string(7) "BIO103L" } } } 
}

This will be sent to view.

Where it should be shown like this

------------------------------------------------------------------
category        |    sub category      |           courselist
------------------------------------------------------------------
                |   Language           |ENG102
                |                      |-------------------------
                |                      | ENG111
University Core |--------------------------------------------------
                |Humanities            |
                |                      |PHI104
                |--------------------------------------------------

I find it very complex and really dont know how to do it.

Any suggestion on how to do it.?

After searching I got that ,I need to use rowspan and colspan . But this array is complex, so I need help.

So far what i have tried, is not even close to the solution . Thats why i didnt show it here.

Try this code :-

<?php
$arr = array(
    "University Core",
    array(
        array("Language", "12", array("ENG102", "ENG111")),
        array("Humanities", "9", array("PHI104")),
        array("Social Sciences", "9", array("ECO101", "ECO104")),
        array("Science", "4", array("BIO103", "BIO103L"))
        ));
$count = 0;
foreach ($arr[1] as $subCat) {
  $count += count($subCat[2]); // to generate total rows
}
?>

<table border="1" width="50%">
  <thead>
    <tr>
      <th width="50%">Category</th>
      <th width="20%">SubCategory</th>
      <th width="15%">Course List</th>
    </tr>
  </thead>
  <tbody>
    <?php 
      foreach($arr[1] as $in => $subCat) { 
        echo ($in === 0)? '<tr><td rowspan="' . $count . '">' . $arr[0] . '</td>': '<tr>'; // get the main category
        foreach($subCat as $key => $value) { 
          echo ($key === 0)? '<td rowspan="' . count($subCat[2]) . '">' . $subCat[$key] . '</td>': ''; // get the sub-category
          if ($key === 2) {
            foreach($value as $course) {
              echo '<td>' . $course . '</td></tr>'; // get the course list
            }
          }
        }
      }
    ?>
  </tbody>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM