Re: RE: Help with my first recursion menu

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

On Sat, Oct 31, 2009 at 11:07 AM, MEM <talofo@xxxxxxxxx> wrote:

>
>
> *From:* Lex Braun [mailto:lex.braun@xxxxxxxxx]
> *Sent:* sábado, 31 de Outubro de 2009 14:05
> *To:* MEM
> *Cc:* php-general@xxxxxxxxxxxxx
> *Subject:* Re:  RE: Help with my first recursion menu
>
>
>
> Hi,
>
> On Wed, Oct 28, 2009 at 5:22 PM, MEM <talofo@xxxxxxxxx> wrote:
>
> I've been told that stack is the way to go, so I'm trying to understand the
> following code:
> http://pastebin.com/m5616c88f
> I've commented every line so that any of you could see if I'm interpreting
> something wrong:
>
>
> I have two questions about this code, that hopefully someone on the list
> could explain:
>
> 1)
> Why do we need to remove the last array item? (on line 32):
> array_pop($urlStack);
>
> On line 20, $url creates an URL path that includes the $cat path.  Once
> you've completed iteration on all children of $cat, the url path should no
> longer include $cat path, thus it is removed from $urlStack.
>
>
>
>
> 2)
> Why we print the closed tag of the li element, after the recursive call?
> (on
> line 29)
> echo "</li>\n";
>
>  Line 29 is closing the li element that was opened on line 23 for $cat.
>
>
>
> Thanks a lot in advance,
> Márcio
>
>  -Lex
>
>
>
>
>
>
>
>
>
> Thanks a lot. I knew that:
>
> “Line 29 is closing the li element that was opened on line 23 for $cat.”
>
>
>
> My question intend to be more like:
>
> Why that line to close the li element is **after** the recursive call?
>
> Somehow, it seems that instead of creating something like this:
>
> <ul>
>
> <li>item1</li>
>
> <li>item2</li>
>
> <li>item3</li>
>
> </ul>
>
>
>
> We are doing:
>
> <ul>
>
> <li>item1
>
> <li>item2
>
> <li>item3
>
> </li>
>
> </ul>
>
>
>
Say you have a tree menu that contains :
$arr = array('item1' => 'Item 1', 'item2' => array('item2a' => 'Item 2a',
'item2b' => 'Item 2b'));
$urlStack = array('category');

When you call the tree() function with the above $arr values, the function
acts as follows:
1. First time through the function, the opening ul element is printed
2. First time through the foreach loop sets $cat = 'item1' and $val = 'Item
1'.
3. As this is not an array, the else condition prints <li>Item 1</li>
4. Second time through the foreach loop sets $cat = 'Item 2' and $val =
array('item2a' => 'Item 2a', 'item2b' => 'Item 2b').
5. As this is an array, the if condition prints <li><a href=''>Item</a> and
calls the tree function recursively
6. Second time through the tree function, another <ul> element is printed
and the two sub items are printed as <li>sub item</li> as they do not
contain additional arrays.  Tree function ends by printing the closing </ul>
tag.
7. The foreach loop from step 5 continues and prints the closing </li>. Tree
function ends by printing the closing </ul> tag.

The output from calling tree($arr, $urlStack) would thus be:
<ul>
<li>Item 1</li>
<li>
<a href="?path=category/Item 2">Item 2</a>
<ul>
<li>Item 2a</li>
<li>Item 2b</li>
</ul>
</li>
</ul>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux