Hello Readers
I am trying to create Categorytree.In which i want to display subcategories under its parent category.I got the code to develop category tree that display using <ul><li> but i want to display it in dropdown. How can i do that?
For Example

$query = mysql_query('SELECT * FROM categories');

while ( $row = mysql_fetch_assoc($query) )

        $menu_array[$row['id']] = array('category' => $row['category'],'parent' => $row['parent']);
		echo "<br>";*/



//recursive function that prints categories as a nested html unorderd list

function generate_menu($parent)


        $has_childs = false;

        //this prevents printing 'ul' if we don't have subcategories for this category

        global $menu_array;

        //use global array variable instead of a local variable to lower stack memory requierment

        foreach($menu_array as $key => $value)


                if ($value['parent'] == $parent) 


                        //if this is the first child print '<ul>'        

                       if ($has_childs === false)


                                //don't print '<ul>' multiple times                             
                                $has_childs = true;
                               echo '<ul>';

                        echo '<li><a href="/category/' . $value['category'] . '/">' . $value['category'] . '</a>';
						echo '</li>';
                        //call function again to generate nested list for subcategories belonging to this category




        if ($has_childs === true) echo '</ul>';

//generate menu starting with parent categories (that have a 0 parent)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.