Input on Category List code please

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.

as a sidenote, you dont even really need the getCategories function - call getSubCategories passing in 0 and ‘’, and start your list outside the function call…

$indent .= $indent;

this line will double whatever you have in $indent. if you have “–” in indent, you’ll get “----”.

I’d have to call <ol> outside of it too right?

And that is not what I thought the operator did, that would be part of the problem.

.= is “Concatenate onto”. $a .= $b is the same as saying $a = $a.$b; In your case, $indent = $indent.$indent;

Should point out that’s the same formula for all Shorthand Operators…

$a <operator>= $b
is the same as saying
$a = $a <operator> $b

Ok, that would explain why it was doubling. Thanks StarLion