0

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

4
  • Why not use JSON? That's what it is for. php.net/manual/en/book.json.php Commented Feb 11, 2013 at 22:06
  • This question has nothing to do with JSON... unless you mean for him to use AJAX and build it client side with Javascript templates. Commented Feb 11, 2013 at 22:13
  • 1
    You really ought to use nested lists for this, but I'll give you a solution below using tabling. Commented Feb 11, 2013 at 22:15
  • @runspired - Nested lists will do the job, but I need to have it in a tabular format, with borders etc, except if I can maybe style the list to look and act like tables, but I'm not sure if it might get a second column in future Commented Feb 11, 2013 at 22:17

1 Answer 1

1

I noticed that your own solution referenced the variable $class, so I've incorporated that too. You really don't need to use CHtml (unless you do, but I've never seen such a case)

function renderItems( $array, $class, $indent = 0  ) {

    foreach( $items as $item) {
        echo '<tr><td class="'.$class.'">'.$item['label'].'</td></tr>';

        if( isset( $item['items'] && count( $item['items'] )
            renderItems($item['items'],$class,$indent+1);
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.