Jump to content

Setting sitemap to variable


Martijn Geerts
 Share

Recommended Posts

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

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 :D I got something wrong.

Edit: updated to really work.

  • Like 1
Link to comment
Share on other sites

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

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 :).

  • Like 2
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
 Share

  • Recently Browsing   0 members

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