Combining functions method:
function dropdown_cats() { global $table_prefix; $main_cats = "SELECT cat_id, cat_name FROM ${table_prefix}categories WHERE cat_parent = 0"; $cats = mysql_query($main_cats); while ( $main_cat_list = mysql_fetch_array($cats) ) { $main_cat = stripslashes($main_cat_list['cat_name']); echo '<option value="' . $main_cat_list['cat_id'] . "\\">$main_cat</option>\\n"; dropdown_sub_cats($main_cat_list['cat_id']); } } function dropdown_sub_cats($main_cat_id, $level = 1) { global $table_prefix; $query = "SELECT cat_id, cat_name FROM ${table_prefix}categories WHERE cat_parent = $main_cat_id"; $sub_list = mysql_query($query); #$num_rows = mysql_num_rows($sub_list); // Required when display in <ul></ul> #if ($num_rows > 0) { while ( $sub_cat_list = mysql_fetch_array($sub_list) ) { $indent = str_repeat('–', $level); $cat_name = stripslashes($sub_cat_list['cat_name']); echo '<option value="' . $sub_cat_list['cat_id'] . "\\">$indent$cat_name</option>\\n"; dropdown_sub_cats($sub_cat_list['cat_id'], $level + 1); } #} }
Single function method:
function list_categories($parent = 0, $level = 0) { global $l, $table_prefix; $results = mysql_query( $l['db_error_query_select'], "SELECT cat_id, cat_name, cat_parent FROM {$table_prefix}categories WHERE cat_parent = $parent " ); $tab = str_repeat("\\t", $level*2); $tab2 = ( $level > 1 ? str_repeat("\\t", $level*2-1) : "\\t"); if (mysql_num_rows($results) > 0) { echo "\\n$tab<ul>"; while ( $r = mysql_fetch_array($results) ) { if ( $parent == $r['cat_parent'] ) { echo "\\n$tab\\t<li><a href=\\"./categories.php?action=view&idn={$r['cat_id']}\\">{$r['cat_name']}</a>"; } list_categories($r['cat_id'], $level+1); echo "</li>"; } echo "\\n$tab</ul>\\n$tab2"; } mysql_free_result($results); }