Help construction if statement

Discussion in 'Programming' started by billybrag, Mar 17, 2006.

  1. #1
    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?
     
    billybrag, Mar 17, 2006 IP
  2. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #2
    Can you post the code you have so far
     
    dct, Mar 17, 2006 IP
  3. billybrag

    billybrag Peon

    Messages:
    324
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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):
     
    billybrag, Mar 17, 2006 IP
  4. TheHoff

    TheHoff Peon

    Messages:
    1,530
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    TheHoff, Mar 17, 2006 IP
  5. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #5
    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?
     
    dct, Mar 17, 2006 IP
  6. billybrag

    billybrag Peon

    Messages:
    324
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    it makes sense but i have no idea how to do it
     
    billybrag, Mar 17, 2006 IP