Category Three How

Discussion in 'PHP' started by PinoyIto, Dec 6, 2006.

  1. #1
    I am trying to display a category three of my site, but I am having problem doing it. I heard that this can be accomplish in single sub query string.... I hope I can get help from here...

    Here are some example of my data
    
    ID             CATEGORY_NAME     PARENTID
    1                Philippines                 0
    2                Abra                        1
    3                Bataan                     1
    4                Abucay                    3
    5                Balanga                    3
    6                Bangued                   2
    7                Pilar                         3
    8                Pilar                         2
    9                Bayan                       7
    
    I want to output them in tree format like this
    
    Philippines
    |__Abra
    |   |___Bangued
    |   |___Pilar
    |__Bataan
        |__Abucay
        |__Balanga
        |__Piliar
            |__Bayan
    
    Code (markup):
    Thanks in Advance
     
    PinoyIto, Dec 6, 2006 IP
  2. smallbuzz

    smallbuzz Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    smallbuzz, Dec 6, 2006 IP
  3. Yeldarb

    Yeldarb Active Member

    Messages:
    209
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #3
    Looks like a good job for a recursive function

    Psuedocode:
    
    yourFunction(array, 1);
    yourFunction(array, int parent) {
    	for(int i=0; i<array.length(); i++) {
    		if(array[i].parent == parent) {
    			echo array[i].name;
    			yourFunction(array, array[i].id);
    		}
    	}
    }
    
    Code (markup):
    Should work.. I think.

    Edit: I forgot to build in the tabbing though. You'll have to do that.
     
    Yeldarb, Dec 6, 2006 IP
  4. PinoyIto

    PinoyIto Notable Member

    Messages:
    5,863
    Likes Received:
    170
    Best Answers:
    0
    Trophy Points:
    260
    #4
    PinoyIto, Dec 6, 2006 IP