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);
?>

Dani AI

Generated

A short, practical follow-up to 's post: HTML <select> cannot represent arbitrarily nested groups — <optgroup> is only useful for a parent + direct children layout and optgroups may not be nested. For deeper trees, render every category as an <option> and show hierarchy with a text prefix (for example -- per level), or build a custom tree picker in JavaScript if you need expand/collapse behavior. (developer.mozilla.org)

Also note the original sample used the old mysql_* API; that extension was deprecated and removed (PHP 7+). Move to PDO or MySQLi and use prepared statements to avoid injection. The PHP manual recommends MySQLi/PDO and documents prepared statements. (php.net)

If you already have a parent/child array (as in the thread), adapt it to print <option> elements with a small recursive routine and htmlspecialchars() on output. Example pattern (PDO + simple recursion):

<?php
// fetch rows into $rows with PDO, then build index by parent
foreach ($rows as $r) $byParent[$r['parent']][] = $r;

function buildOptions($parent = 0, $depth = 0) {
    global $byParent;
    if (empty($byParent[$parent])) return;
    foreach ($byParent[$parent] as $node) {
        $prefix = str_repeat('--', $depth);
        echo '<option value="'.htmlspecialchars($node['id']).'">'
           . $prefix . htmlspecialchars($node['category']) . "</option>\n";
        buildOptions($node['id'], $depth + 1);
    }
}

echo "<select>\n";
buildOptions(0);
echo "</select>\n";
?>

Use this for arbitrary depth; use <optgroup> only when you have strictly one parent level to group. (developer.mozilla.org)

Solved

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.