Jump to content

Need help on a recursive function to output a list tree with extra logic


Recommended Posts

I'm creating a recursive PHP function for showing a 3 level menu.

- child
- child
- - gc
- - gc
- - - ggc
- - gc
- child
- child
- - etc

This is all easy stuff with the recursive function but the problem is I want to use some logic that has to bubble up from the great grandchildren to the parents.

If one of the menu items does not have any related pages, I don't want to show it. Like an empty category in a blog or something. Again this is easy to just test for during the loops.

It gets tricky at the next part. If a parent or grandchild is empty, it will still have to show up if one of its children has a related item. So if any child has an item, its parent must show. And likewise, if some great grandchild has an item, it's parent and grandparent have to both show.

A work in progress of the function looks something like this, I've removed some logic to clean it up like assembling the href and classes and so forth. This is the gist of it.

function menu($p = null, $ignore = false) {
	echo '<ul>';
	foreach ($p->children() as $c) {
		// $ignore contains a pageArray of items that can be ignored, already determined that these are empty
		// it's just that I don't want to ignore it if a child or grandchild DOES have items
		if (is_object($ignore) && !$ignore->has("id={$c->id}")) { continue; }
		echo '<li><a href="HREF" class="CLASSES">' . $p->title . '</a>';
		// Recursive call here
		if ($c->hasChildren()) { menu($c, $ignore); }
		echo "</li>";
	echo '</ul>';


Link to comment
Share on other sites

Sounds like you need to keep track of the state of the children that have content using a  separate list or array or whatever data structure you see fit.
I assume each item is uniquely identifiable? 
If so, you can add to this structure all items that has the related item or content.

I guess when you hit the logic whether you determine a parent/grandparent needs to be shown, you need to refer to this structure and do a membership test.

i.e. Are any items in this list a child or grandchild of mine.

 If it's in this structure, you know you have to show it. If not, you don't have to.

Link to comment
Share on other sites

Or alternatively, if you can associate a field with each menu item say "show".

You would loop through each children, tagging anything to show.
When you loop through up the chain, you check if the children has something to show, and if it does it gets tagged as show.

Only thing I'm not clear is if there's a scenario where a parent has to be shown but there's no content for its child.

You may need to check for this scenario separately, and if such conditions are met, you will need to recurse back down to the children and tag it to show.

Link to comment
Share on other sites

Hello for all.

On 1/3/2018 at 3:22 AM, Vigilante said:

This is all easy stuff with the recursive function but the problem is I want to use some logic that has to bubble up from the great grandchildren to the parents.


Maybe this module can help in that situation?

How it's works and what it's does:

1. it use customised nestable jquery plugin

2. it clone current PW pages tree

3. provide drag and drop options with parents and childs

4. provide easy option to manipulate with tree (eg. hide children from desired parents)

5. store pages tree list in JSON format, and every list item has attributes (page id and "slug")

6. don't need to build desired website pages tree on-the fly (with complex logic and if statements) in page rendering process


Note: to render menu need to create simple function (eg. inside _func.php )

Maybe looks complicated at first sight, but I think it's worth a try.

Link to description and download.


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...