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
Parent1
__Child1-Parent1
__Child2-Parent1
__|__Child1-Child2-Parent1
Parent2
__Child1-Parent2
__Child2-Parent2
__|__Child1-Child2-Parent2
Parent3
Parent4
__Child-parent4

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

while ( $row = mysql_fetch_assoc($query) )

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


?>

<?php

//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>';
						
                        generate_menu($key);
						
						
                        //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)

generate_menu(0);
?>
Be a part of the DaniWeb community

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