Jump to content

Troubleshooting PHP concatenate assignment operator for sitemap


MarcC
 Share

Recommended Posts

I wanted to run this by the forum, see if anybody can help. I think it may be a concatenation operator problem, which I don't have much experience with. I'm trying to convert the default sitemap.php code to something that outputs into a variable.

If I run the following code, I only get a link to "Home" and then a bunch of <!-- this line prints OK -->. Any tips would be appreciated.

 
function sitemapListPage($page) {
$siteMap .= "<li><a href='{$page->url}'>{$page->title}</a> "; 
 
if($page->numChildren) {
$siteMap .= "<ul>";
foreach($page->children as $child) {
$siteMap .= "<!-- this line prints OK -->";
sitemapListPage($child);
}
$siteMap .= "</ul>";
}
 
$siteMap .= "</li>";
return $siteMap;
}
 
$outMain .= "<ul class='sitemap'>";
$outMain .= sitemapListPage($pages->get("/")); 
$outMain .= "</ul>";
Link to comment
Share on other sites



<?php
function sitemapListPage($page,$siteMap='') {
    $siteMap .= "<li><a href='{$page->url}'>{$page->title}</a> "; 

    if($page->numChildren) {
        $siteMap .= "<ul>";
        foreach($page->children as $child) {
            $siteMap .= "<li><a href='$child->url'>$child->title</a></li>";
            sitemapListPage($child,$siteMap);
        }
        $siteMap .= "</ul>";
    }

    $siteMap .= "</li>";
    return $siteMap;
}

$outMain .= "<ul class='sitemap'>";
$outMain .= sitemapListPage($pages->get("/")); 
$outMain .= "</ul>";

You need to pass along the variable. $siteMap='' argument is default to avoid passing an empty one. If defined it will get overwritten ans passed along.

  • Like 1
Link to comment
Share on other sites

function siteMap($page, $output='') {
    $output .= "<li><a href='{$page->url}'>{$page->title}</a>";
    if($page->numChildren) {
        $output .= "<ul>";
        foreach($page->children as $child) $output .= siteMap($child);
        $output .= "</ul>";
    }
    $output .= "</li>";
    return $output;
}

$homepage = $pages->get("/");

$siteMap = "<ul>" . siteMap($homepage) . "</ul>";

Think this will work

Lesson:

- first look if Soma, Diogo, Nik, Ryan, etc. etc. are active on the forum.

- then, don't post at all ( cause they will be first ) :)

Edited by Martijn Geerts
  • Like 3
Link to comment
Share on other sites

Actually had the same issue as you had marc. Soma solved that for me then.

I left out the tab & new-line as it provides more flexibility in CSS. 

for example:

ul {
    display: block;
    text-align: center;
}

li {
    display: inline;
}

This way you don't have the extra "white space" between the li items & you can text-align the li's.

For navigations etc. I use a modified MarkupSimpleNavigation from Soma that don't output any tab, newline or any other white-space thingy. Hopefully Soma will simplify his module & leave them out by default.

(I only use generated source if I'm not ripping JS )

example: http://codepen.io/anon/pen/FDrim

 
Edited by Martijn Geerts
  • Like 1
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...