I'm having difficulties with the following scenario, I have an array with categories, so categories can have child categories and the child categories can have child categories infinitely. Now what I'm trying to achieve is the following, but I just can't manage to achive this.
I have an array $items in the following structure
Array
(
[0] => Array
(
[label] => Main Cat
[id] => 29
[parent_id] => 19
)
[1] => Array
(
[label] => Main Cat
[id] => 17
[parent_id] => 19
)
[2] => Array
(
[label] => Main Cat
[id] => 20
[parent_id] => 19
[items] => Array
(
[0] => Array
(
[label] => Child Level 1
[id] => 21
[parent_id] => 20
)
[1] => Array
(
[label] => Child Level 1
[id] => 22
[parent_id] => 20
[items] => Array
(
[0] => Array
(
[label] => Child Level 2
[id] => 27
[parent_id] => 22
[items] => Array
(
[0] => Array
(
[label] => Child Level 3
[id] => 28
[parent_id] => 27
)
)
)
)
)
[2] => Array
(
[label] => Child Level 1
[id] => 23
[parent_id] => 20
)
[3] => Array
(
[label] => Child Level 1
[id] => 24
[parent_id] => 20
[items] => Array
(
[0] => Array
(
[label] => Child Level 2
[id] => 25
[parent_id] => 24
)
[1] => Array
(
[label] => Child Level 2
[id] => 26
[parent_id] => 24
)
)
)
)
)
)
Now I want the following array to display in a table with each child level indented, and if it should go one level up from Child Level 3 to Child Level 2 the indetation should reverse again.
Example
<table border="2" width="100%">
<tr>
<td>Main Cat</td>
</tr>
<tr>
<td>Main Cat</td>
</tr>
<tr>
<td>Main Cat</td>
</tr>
<tr>
<td style="margin-left:20px;">Child Level 1</td>
</tr>
<tr>
<td style="margin-left:20px;">Child Level 1</td>
</tr>
<tr>
<td style="margin-left:40px;">Child Level 2</td>
</tr>
<tr>
<td style="margin-left:60px;">Child Level 3</td>
</tr>
<tr>
<td style="margin-left:20px;">Child Level 1</td>
</tr>
<tr>
<td style="margin-left:20px;">Child Level 1</td>
</tr>
<tr>
<td style="margin-left:40px;">Child Level 2</td>
</tr>
<tr>
<td style="margin-left:40px;">Child Level 2</td>
</tr>
</table>
My PHP code looks like this thus far,
public function renderCategoriesRecursive($items)
{
foreach($items as $item)
{
$itemCount = count($item['items']);
echo CHtml::openTag('tr');
echo CHtml::openTag('td',array('class'=>$class));
echo $item['label'];
echo CHtml::closeTag('tr');
echo CHtml::closeTag('tr');
if(isset($item['items']) && $itemCount)
{
$this->renderCategoriesRecursive($item['items']);
}
}
}
The variable $items contains the array above