Category Dropdown with Visual Nested Subcategories
This code must be placed in a template with PHP parsing set to “on”. You need to change weblog1 to your weblog shortname and modify the {path="weblog/index"} to whatever template you use to display your categories.
<?php
global $DB, $cat_array;
$cat_array = array();
$sql = "SELECT exp_categories.group_id, exp_categories.parent_id,
exp_categories.cat_id, exp_categories.cat_name
FROM exp_categories, exp_category_groups, exp_weblogs
WHERE exp_category_groups.group_id = exp_categories.group_id
AND exp_categories.group_id = exp_weblogs.cat_group
AND exp_weblogs.blog_name = 'weblog1'
ORDER BY group_id, parent_id, cat_order, cat_name";
$query = $DB->query($sql);
if ($query->num_rows > 0) {
foreach ($query->result as $row) {
$categories[$row['cat_id']] = array($row['group_id'], $row['cat_name'], $row['parent_id']);}
foreach($categories as $key => $val) {if (0 == $val['2']) {
$cat_array[] = array($val['0'], $key, $val['1']);
category_subtree($key, $categories, $depth=1);}}}
// Category Sub-tree START
function category_subtree($cat_id, $categories, $depth) {
global $cat_array;
$spcr = " ";
$indent = " ";
if ($depth == 1) {$depth = 2;}
else {
$indent = str_repeat($spcr, $depth).$indent;
$depth = $depth + 4;}
$sel = '';
foreach ($categories as $key => $val) {if ($cat_id == $val['2']) {
$pre = ($depth > 2) ? " " : '';
$cat_array[] = array($val['0'], $key, $pre.$indent.$spcr.$val['1']);
category_subtree($key, $categories, $depth);}}}
// Category Sub-tree END
echo '<form action="">
<select name="categorical" onchange="
.href=\'{path="weblog/index"}C\'
+ this.form.categorical.options[this.form.categorical.selectedIndex].value + \'/\';">
<option value="">Categories</option>';
foreach($cat_array as $value)
{echo '<option value="'.$value['1'].'">'.$value['2'].'</option>'."\n";}
echo '</select>
</form>';
?>
