Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to build a dynamic table wherein the columns can be split upon a click. For example, if you click on Split on column 1 in layer 1, column 1 will have two child columns = 2nd layer. When you click on split again in layer 1, one column will be added in layer 2, so on and so forth. However, now I am stuck with how to determine the right colspan depending on the maximum value set by the column with the highest number of child columns.

In the block of code here, I am simply loading data from the database. Data is passed from a model to the view where the table is created.

What I have tried:

So far this is what I have:

HTML
<table border=1>
      <tr>
      <?php
        $parentcols = array();
        $maxcolspan = 0;

        //loop through parent columns in layer 1
        foreach($parentcoldata as $parentkey)
        {
          $children = array();

          foreach($childcoldata as $childcol)
          {
          //check if the current child col is a child of the current parent col
            if ($childcol->parentcol_id == $parentkey->id)
            {
                $curchild = array("childname" => $childcol->name, "layer_id" => $childcol->layer_id);
                $children[$childcol->id] = $curchild;
            }
           
          }
          
          //know number of children of current parent column
          $childnum = count($children);
          if ($childnum == 0) {unset($children); echo "Unset";}

          //set colspan based on column with the highest number of children
          if ($childnum > $maxcolspan)
          {
            $maxcolspan = $childnum;
          }
          
          //save the details about the parent column
          $parent = array("name" => $parentkey->name,
                          "child" => $childnum,
                          "children" => $children,
                          "id" => $parentkey->id
                        );
        }

        //print row for parent columns - 1st layer
        for ($i=1; $i<=count($parentcols); $i++) {
            echo '<th id="parentcol'.$parentcols[$i]['id'].'" colspan='.$parentcols[$i]['child'].' style="padding: 5px">'.$parentcols[$i]['name'].'<br />'.
            '<a href="">Edit</a> <br />'.
            '<a href="">Delete</a> <br />'.
            '<a href="">Split</a>'.
            '</th>';
        }
        ?>
      </tr>

      <?php
      //this is where i attempt to print the child columns
      for ($index = 1; $index <= $maxcolspan-1; $index++)
      {
        echo "<tr>";

          foreach($parentcols as $parentcolid => $value) //$parentcolid = key/id
          {
            //loop through children columns
            foreach($value['children'] as $children => $child)
            {
              if ($child['layer_id'] == $index){
                  echo '<td id="'.$children.'" style="padding:5px">'.$child['childname'].
                  '<a href="">Edit</a> <br />'.
                  '<a href="">Delete</a> <br />'.
                  '<a href="" parent-id="someparentid">Split</a>'
                  .'</td>';
              }

            }
          }
          echo "</tr>";
        }
         ?>
    </table>


But with this code, the child columns (3rd layer) of the child columns in the 2nd layer no longer get included.

I am stuck and not sure what I am missing.

Table structure:
columns[^]
Posted
Updated 18-Dec-16 2:22am
v8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900