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:
<table border=1>
<tr>
<?php
$parentcols = array();
$maxcolspan = 0;
foreach($parentcoldata as $parentkey)
{
$children = array();
foreach($childcoldata as $childcol)
{
if ($childcol->parentcol_id == $parentkey->id)
{
$curchild = array("childname" => $childcol->name, "layer_id" => $childcol->layer_id);
$children[$childcol->id] = $curchild;
}
}
$childnum = count($children);
if ($childnum == 0) {unset($children); echo "Unset";}
if ($childnum > $maxcolspan)
{
$maxcolspan = $childnum;
}
$parent = array("name" => $parentkey->name,
"child" => $childnum,
"children" => $children,
"id" => $parentkey->id
);
}
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
for ($index = 1; $index <= $maxcolspan-1; $index++)
{
echo "<tr>";
foreach($parentcols as $parentcolid => $value)
{
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[
^]