0

I have an Array to be displayed in a CakePHP View which contains the navigation data. Each array item contains fields like id, name, level etc. as well as another array field named children, where this structure repeats.

In the Cake PHP View it's easy to walk over the array on the first level with a foreach loop and the html-Helper to output the relevant data. But how do I ouput the child items (and then their children if present)? Normally I would do a recursive function call, but within the view you shouldn't use function look, should you? Bunt within the controller I cannot use the html helper, so I'm stuck here.

3 Answers 3

1

I think as a clean solution to make a helper to iterate over the array an recursively look for each elements if one of them another array (http://book.cakephp.org/#!/view/1097/Creating-Helpers)

Or if you know that your item could has one and only one child, you could ask if the actual item is an array an operate it. But I prefer the first solution.

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

Comments

0

It looks like you might try the tree behavior

http://book.cakephp.org/#!/view/1341/Basic-Usage

1 Comment

tree behavior does not help much in the view, also i would guess this is being used as the data is already nested.
0

you need something like a recursive method that can call it's self, something like the following. https://github.com/infinitas/infinitas/blob/beta/core/menus/views/helpers/menu.php#L163

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.