Martijn Geerts Posted August 16, 2012 Share Posted August 16, 2012 I'm trying to set a sitemap to a variable, but somehow it doesn't function. It's unexperienced me having "trouble" with PHP. function siteMap($page) { $output = "<ul>"; function sub($page, $output) { $output .= "<li><a href='{$page->url}'>{$page->title}</a>"; if($page->numChildren) { $output .= "<ul>"; foreach($page->children as $child) sub($child, $output); $output .= "</ul>"; } $output .= "</li>"; } sub($page, $output); $output .= "</ul>"; return $output; } $siteMap = siteMap($pages->get("/")); Who can help me out explaining why it isn't functioning. tnx Link to comment Share on other sites More sharing options...
Soma Posted August 16, 2012 Share Posted August 16, 2012 You're having a function in a function and some complexity not really needed here. This is the simplest way: function siteMap($page, $output='') { $output .= "\n\t<li><a href='{$page->url}'>{$page->title}</a>"; if($page->numChildren) { $output .= "\n\t\t<ul>"; foreach($page->children as $child) $output .= str_replace("\n", "\n\t\t", siteMap($child)); $output .= "\n\t\t</ul>"; } $output .= "\n\t</li>"; return $output; } $homepage = $pages->get("/"); $siteMap = siteMap($homepage); echo "<ul>".$siteMap."</ul>"; Edit: Screw it I got something wrong. Edit: updated to really work. 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted August 16, 2012 Author Share Posted August 16, 2012 Thanks Soma, you just disarmed an explosive head. [[ $output .= siteMap($child); ]] didn't even know that's posible. Link to comment Share on other sites More sharing options...
Soma Posted August 16, 2012 Share Posted August 16, 2012 Ok got it. I updated the code above, it produces valid nested ul and even some indentation. Don't even ask why it works, it just works. It's magic. 1 Link to comment Share on other sites More sharing options...
Soma Posted August 16, 2012 Share Posted August 16, 2012 For more easier (without exploding head) sitemap generation take a look at the MarkupSimpleNavigation module. http://modules.processwire.com/modules/markup-simple-navigation/ Link to comment Share on other sites More sharing options...
Martijn Geerts Posted August 16, 2012 Author Share Posted August 16, 2012 I needed the "sitemap" script for a horse pedigree tree, the original is more complex. As every horse has a field of type Page for the father ( create if not exists ) and for the mother the same. But again, learned a little more today. B) Think when I have a simpler site structure I will try out your MarkupSimpleNavigation. Link to comment Share on other sites More sharing options...
nik Posted August 24, 2012 Share Posted August 24, 2012 Your problem got solved a while ago, but a bit of information was left missing: the actual reason your code didn't work as expected. The original version is a bit complex, yes, but it's actually only a single ampersand away from working. (Well, I didn't read the output carefully through so there could be some other flaws as well). The problem is that $output is being passed by value to sub() when it needs to be passed by reference for sub() to be able to change it. So changing the function definition to "function sub($page, &$output)" does the trick. More on passing by reference for example at php.net article (not so good a reference though). But this is just to point out what was wrong in the first place in case someone was left wondering, as the code may seem right at the first glance. Of course you could modify it a bit in another way as well (returning the $output and assigning it back to where it belongs would work too) but let's leave it here as better solutions were already given by Soma more than a week ago . 2 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted August 25, 2012 Author Share Posted August 25, 2012 Sorry that I missed your post. Next time when i'm in the same scenario hopefully I think of what you said. Big THANX for the explaining. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now