Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a php code that outputs the correct html navigation structure. However, when added to the css clases, the structure disappears.

My PHP Code:

public function show_public($items, $parent = NULL)
    {
        $hasChildren = false;
        $outputHtml = '<ul class="dropdown-menu">%s</ul>';
        $childrenHtml = '';

        foreach($items as $item)
        {
            if ($item['pid'] == $parent) {
                $hasChildren = true;
                $childrenHtml .= '<li class="dropdown">';         
                $childrenHtml .= '<a class="dropdown-toggle" href="#">' . $item['name'] . '</a>';         
                $childrenHtml .= $this->show_public($items, $item['id']);         
                $childrenHtml .= '</li>';           
            }
        }

        // Without pid don`t need the <ul>.
        if (!$hasChildren) {
            $outputHtml = '';
        }

        return sprintf($outputHtml, $childrenHtml);
    }

I want to create Html output like this: http://jsfiddle.net/XY8c6/

My Navigation DB Table:

navigation

If parent_id == Null, then $this->id is parent ( root cat)

Ir parent_id !== Null, then $this->id is child of $this->parent.

(I hope that this explanation can be understood)

PHP array which creates the structure:

        Array
    (
        [1] => Array
            (
                [id] => 4
                [name] => Home
                [pid] => 
                [url] => /
            )
        [2] => Array
            (
                [id] => 8
                [name] => Room
                [pid] => 4
                [url] => room
            )
        [3] => Array
            (
                [id] => 9
                [name] => WC
                [pid] => 4
                [url] => wc
            )
        [4] => Array
            (
                [id] => 10
                [name] => Soap
                [pid] => 9
                [url] => soap
            )
        [5] => Array
            (
                [id] => 1
                [name] => About Me
                [pid] => 
                [url] => about
            )
        [6] => Array
            (
                [id] => 7
                [name] => About My dog
                [pid] => 1
                [url] => dog
            )
        [7] => Array
            (
                [id] => 2
                [name] => Contacts
                [pid] => 
                [url] => contacts
            )
    )

Q: How to properly create a recursive function, who be able to generate an unlimited depth of childrens?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.