How To Generate List Html

I have create an table in db call tree (id, name, parent_id)

id is identifier of each record

name is the name of each record

parent_id: if a record doesn’t have any child, it has value 0,

       otherwise it has value of it parent's id

I need to generate something similiar to this:


                      <ul>

                            <li id='home'>Home</li>

                            <li item-checked='true' item-expanded='true'>Solutions

                                <ul>

                                    <li>Education</li>

                                    <li>Financial services</li>

                                    <li>Government</li>

                                    <li item-checked='false'>Manufacturing</li>

                                    <li>Solutions

                                        <ul>

                                            <li>Consumer photo and video</li>

                                            <li>Mobile</li>

                                            <li>Rich Internet applications</li>

                                            <li>Technical communication</li>

                                            <li>Training and eLearning</li>

                                            <li>Web conferencing</li>

                                        </ul>

                                    </li>

                                    <li>All industries and solutions</li>

                                </ul>

                            </li>

                            <li>Products

                                <ul>

                                    <li>PC products</li>

                                    <li>Mobile products</li>

                                    <li>All products</li>

                                </ul>

                            </li>

                            <li>Support

                                <ul>

                                    <li>Support home</li>

                                    <li>Customer Service</li>

                                    <li>Knowledge base</li>

                                    <li>Books</li>

                                    <li>Training and certification</li>

                                    <li>Support programs</li>

                                    <li>Forums</li>

                                    <li>Documentation</li>

                                    <li>Updates</li>

                                </ul>

                            </li>

                            <li>Communities

                                <ul>

                                    <li>Designers</li>

                                    <li>Developers</li>

                                    <li>Educators and students</li>

                                    <li>Partners</li>

                                    <li>By resource

                                        <ul>

                                            <li>Labs</li>

                                            <li>TV</li>

                                            <li>Forums</li>

                                            <li>Exchange</li>

                                            <li>Blogs</li>

                                            <li>Experience Design</li>

                                        </ul>

                                    </li>

                                </ul>

                            </li>

                            <li>Company

                                <ul>

                                    <li>About Us</li>

                                    <li>Press</li>

                                    <li>Investor Relations</li>

                                    <li>Corporate Affairs</li>

                                    <li>Careers</li>

                                    <li>Showcase</li>

                                    <li>Events</li>

                                    <li>Contact Us</li>

                                    <li>Become an affiliate</li>

                                </ul>

                            </li>

                        </ul>



Can anyone give me any idea for this issue?

I do it in two steps. First I’m building a tree like structure using arrays and references. Then I pass it to a recursive method that draws the tree.

Building the tree:




        $items = Tree::model()->findAll();

        $flat = array();

        $top = array();

        foreach($items as $item) {

            $primary_key = $item->getPrimaryKey();

            $parent_key = $item->parent_id;

            $flat[$primary_key] = array(

                'object'=>$item,

                'children'=>array(),

                'parent'=>null,

            );

            if ($parent_key !== null) {

                $flat[$parent_key]['children'][$primary_key] = &$flat[$primary_key];

                $flat[$primary_key]['parent'] = &$flat[$parent_key];

            } else {

                $top[$primary_key] = &$flat[$primary_key];

            }

        }



Now the root of the tree is in the $top variable.

Warning! You have to be careful with references. Such arrays can’t be logged using var_dump, serialized or encoded using json_encode because they contain circular references.

Remove the ‘parent’ key if not needed to avoid trouble.




    public function renderTree(array $tree, $parent_id = 'root', $level = 1) {

        $output = CHtml::tag('ul', array(), '', false);

        foreach($tree as $id=>$leaf) {

            $listItem = CHtml::link($leaf['object']->name, '#', array());

            if (!empty($leaf['children']))

                $listItem .= $this->renderTree(

                    $leaf['children'],

                    $leaf['object']->$idProperty,

                    $level+1

                );

            $output .= CHtml::tag('li', array(), $listItem);

        }

        $output .= '</ul>';

        return $output;

    }