I would nest a second recorset inside of the select list inside of the do() loop that gets the subcategories and loop on that as well. That way you can see how many results the subcategory recordset has and only show the parent category when it has no children.