Thanks to Rudy Limeback's tutorial about Categories and Subcategories and the Simply SQL book, I am writing better sql and reducing my queries in my scripts...

But I am having an issue with displaying this format using Rudy's first example from Displaying all categories and subcategories: site maps and navigation bars

My query gives me this
animal birdie NULL NULL
animal doggie companion chihuahua
animal doggie companion poodle

And I need PHP to output
animal
- birdie
- doggie
-- companion
--- chihuahua
--- poodle

But it gives me
animal
animal
- birdie
animal
- doggie
etc etc

Any tips on how make this happen the correct way (and faster, my code is very bloated..)?