Hello, I’ve written the below code to display categories and it works, displaying only categories that contain posts and displaying them thus:
Category
-Subcategory
–SubSubcategory
The issue I’m having is getting the “-” to appear in the correct amount for the tier, it either goes as far as two or starts doubling depending on how I do it. Plus the process just seems unwieldy. Can anyone suggest a way I could improve this method?
function getCategories() {
$result = mysql_query("SELECT category_id, category_name FROM categories WHERE category_parent = 0 AND category_count > 0 ORDER BY category_count DESC") or die(mysql_error());
print "<h3>Categories</h3>
<ol>";
while($row = mysql_fetch_array($result)) {
$id = stripslashes($row['category_id']);
$title = stripslashes($row['category_name']);
$title = ucwords($title);
print "<li><a href='"."/index.php?category=".$id."' alt='".$title."'>".$title."</a></li>";
getSubCategories($id);
}
print "</ol>";
}
function getSubCategories($parentCat, $indent='-') {
$result = mysql_query("SELECT category_id, category_name FROM categories WHERE category_parent = $parentCat AND category_count > 0 ORDER BY category_name ASC") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$id = stripslashes($row['category_id']);
$title = stripslashes($row['category_name']);
$title = ucwords($title);
print "<li>".$indent." <a href='"."/index.php?category=".$id."' alt='".$title."'>".$title."</a></li>\
";
$indent .= $indent;
getSubCategories($id, $indent);
}
}
Thanks in advance.