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:


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

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



                                    <li>Financial services</li>


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



                                            <li>Consumer photo and video</li>


                                            <li>Rich Internet applications</li>

                                            <li>Technical communication</li>

                                            <li>Training and eLearning</li>

                                            <li>Web conferencing</li>



                                    <li>All industries and solutions</li>





                                    <li>PC products</li>

                                    <li>Mobile products</li>

                                    <li>All products</li>





                                    <li>Support home</li>

                                    <li>Customer Service</li>

                                    <li>Knowledge base</li>


                                    <li>Training and certification</li>

                                    <li>Support programs</li>










                                    <li>Educators and students</li>


                                    <li>By resource







                                            <li>Experience Design</li>







                                    <li>About Us</li>


                                    <li>Investor Relations</li>

                                    <li>Corporate Affairs</li>




                                    <li>Contact Us</li>

                                    <li>Become an affiliate</li>




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(





            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(





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


        $output .= '</ul>';

        return $output;