Ok so i have a db that is spitting out categories, each of these has a PARENT_ID . If the value of PARENT_ID = 0 then this is a root category, otherwise it identifies the parent category it belongs to. so what i am trying to do is make an <UL> to display these categories so that if a category has child categories these will be displayed as a nested <UL> so it would look something like (arrows purely for illustration) ->Cat1 ->cat2 ->cat3 ---->Cat3.1 ---->cat3.2 ->cat4 ->etc.. ->etc.. i have tried playing with if statements to do this, which has worked but not placed the sub categories in the right place can anyone help?
sure - its coldfusion, but the if statements would be the same <ul> <cfoutput query="getcategories"> <cfif #PARENT_ID# EQ 0> <li><a href="/category-page.cfm?cat_id=#ID#"> #title#</a></li> <cfelse> <ul> <li><a href="/category-page.cfm?cat_id=#ID#"> #title#</a></li> </ul> </cfif> </cfoutput> </ul> Code (markup): so the if statement atm is basically <ul> if PARENT_ID=0 { <li><a href="/category-page.cfm?cat_id=#ID#"> #title#</a></li> } else { <ul> <li><a href="/category-page.cfm?cat_id=#ID#"> #title#</a></li> </ul> } </ul> Code (markup):
I've done this... pain in the ass if you have unlimited categories. - Set a global variable that counts how many levels deep you are. Zero that variable out when you start a new parent category. Each time you go a level deeper in the children, add one to the variable. This allows you to track how many levels in you are and what sort of indenting you should be doing. - Recursively sort through your categories. If parentid!=0, recurse some more until you find the deepest child level. - What I do is shrink the font size by a px for each level I go down, until it reaches a bottom level say 9 or 10px. Also the margin for each LI gets larger so it indents itself, also to a limit.. I usually do this with CSS classes. So each level you go down, set a new CSS class on the LI and have it indented.
I don't know Cold Fusion, but what you'll need to do is in your code store the last parentID and when it changes from 0 to a different ID open a new <ul> and then when it changes back to 0 close the </ul>. Does that make sense?