-1

I having some problems generating a full tree of my db(sitemap).
I reached a level that i want to write better code. Thats why i choose a recursive function for this puzzle.

I want to generate a endless unordered list. Like so:

<ul>
 <li><a></a>
  <ul>
    <li><a></a>(subs)
    etc etc etc.....
  </ul>
 </li>
</ul>

Like i already said i tried the following:

<?php
    function traverseArray($array, $sub=false)
    { 
        foreach($array as $cat)
        {
            if(isset($cat['childeren']) && is_array($cat['childeren']))
            {
                //a category with subs
                echo('<ul id="'.$cat['parent_cat_id'].'" class="lv0">');
                    echo('<li id="'.$cat['parent_cat_id'].'"><a href=#>'.$cat['name'].'</a>'."\n");
                traverseArray($cat['childeren'], true);
            }else{
                if($sub){
                    //a sub category of category
                    echo('</ul></li>');
                    echo('<li id="'.$cat['parent_cat_id'].'"><a href=#>'.$cat['name'].'</a></li>'."\n");                
                }else{
                    //category with no subs
                    echo('<ul id="'.$cat['parent_cat_id'].'" class="lv0">');            
                        echo('<li id="'.$cat['parent_cat_id'].'"><a href=#>'.$cat['name'].'</a></li>'."\n");
                    echo('</ul>');
                }
            }
        }
    }
    traverseArray($sitemap);
?>

but this is a puzzle i cant figure out properly, this the result so far(messy):

<ul id="0" class="lv0">
   <li id="0"><a href=#>Headsets</a>
</ul></li>

<li id="1"><a href=#>(USB) headsets ....</a></li>
</ul></li>
<li id="1"><a href=#>... headsets</a></li>
</ul></li>
<li id="1"><a href=#>.. USB headsets</a></li>
</ul></li>
<li id="1"><a href=#>Bluetooth headsets</a></li>
<ul id="0" class="lv0">
   <li id="0"><a href=#>Unified Communications</a></li>

A big mess! The $sitemap array looks like this:

Array
(
    [0] => Array
        (
            [category_id] => 1
            [parent_cat_id] => 0
            ..........etc
            ........etc
            [type] => cat
            [childeren] => Array
                (
                    [0] => Array
                        (
                            [category_id] => 2
                            [parent_cat_id] => 1
                            .......etc
                            [type] => cat
                            [childeren] => Array
                                (
                                    [0] => Array
                                        (
                                            [category_id] => 32
                                            [parent_cat_id] => 16
                                            .......etc
                                            [type] => series
                                        )                            
                        )

So childeren in childeren in childeren, Is this the best way to do this/ on the right track?

Or are there beter ways out there? Like sql tree's or something? or just again the old foreach in foreach ..(which i am trying to avoid this time).

Any help would be much appreciated!!!

Thanks in advance,

Jacob

Jacob
  • 579
  • 1
  • 5
  • 14
  • find some usefull msql query on this site: Still dont know if this is the right way to do this, http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ – Jacob Jan 23 '13 at 16:14
  • Please help me to solve my issue(http://stackoverflow.com/questions/17673917/confused-to-make-sitemap-using-php-coding) related to this same question – user2173803 Jul 16 '13 at 19:05

1 Answers1

0

"What is wrong with my code?" is not a question for stackoverflow.

Think about what 'traverseArray' function should do. Your code is a mess. Here is a cleaned example of array traversing. Please fit it to your needs:

function traverseArray($array)
{
    echo '<ul>';
    foreach($array as $cat)
    {
        echo '<li>';
        if(isset($cat['childeren']) && is_array($cat['childeren']))
        {
            echo '<a href="#">' . $cat['name'] . '</a>';
            traverseArray($cat['childeren']);
        }
        else
        {
            echo '<a href="' . $cat['url'] . '">' . $cat['name'] . '</a>';
        }
        echo '</li>';
    }
    echo '</ul>';
}

traverseArray($sitemap);
fsw
  • 3,595
  • 3
  • 20
  • 34
  • Thanks for answering, However, I do know that this is not the site for asking about whats wrong with my code! maybe i pronounced the question wrong and should it be something liked this, How do you reverse(array) down a tree and output nested list? Or something in that manner. I did also know that my code was a mess, told you so. I even tried this option too at the very beginning. it started to get a mess when i wanted to get the first item of the tree outputted, because now you get the childeren only. – Jacob Jan 24 '13 at 09:18
  • added outputing first element of a tree. please check. – fsw Jan 24 '13 at 12:14
  • Great! Thanks, couldn't figure it out by my self (still cant get my head around it). – Jacob Jan 25 '13 at 08:51