Recursive function and formatting string for javascript tree

From: Date: Tue, 24 May 2005 15:41:46 +0000
Subject: Recursive function and formatting string for javascript tree
Groups: php.general 
Request: Send a blank email to php-general+get-215799@lists.php.net to get a copy of this message
Hi all. I can't seem to figure this one out. I am trying to output a string for a javascript that builds a DHTML tree. The sample string looks like this: [
    ['Executive Director', null, null,
        ['Executive Assistant ', null, null],
        ['Operations Director', null, null,
            ['Information Technology Director', null, null,
                ['Information Technology Analyst', null, null]
            ]
        ],
        ['Finance Director', null, null],
        ['Human Resources Director', null, null],
        ['Program Services Director', null, null]
    ]
] My class contains these two functions to make this happen:
    function generateOrg() {
        $this->getDepts(0,1);
        $str = "[". $this->str."]";
        return ($str);
    }
    function getDepts ( $parent, $level ) {
        $sql = 'SELECT BudgetedOrganization.* ';
        $sql .= 'FROM BudgetedOrganization ';
        $sql .= 'WHERE BudgetedOrganization.boSuperiorOrgID = ' . $parent;
        $rs = $this->retrieveData($sql);
        $totalRows = mysql_num_rows($rs);
        if ($totalRows > 0)
        {
            while($row = mysql_fetch_array($rs)){
                $currRow = 0;
                $this->str .= "['" . $row['boOrgName'] . "',null,null";
                ($totalRows == $currRow) ? $delim = "," : $delim = "],";
                $this->str .= $delim;
                $this->getDepts($row['boOrgID'],$level+1);
                $currRow++;
            }
        }
        return($this->str);
    }
Here is the output I am getting from my database, and the format is not right. I have played with it for hours and still can't figure out how to get the brackets and commas formatted properly to represent the data tree properly. ['Organization XYZ',null,null],
    ['Operations',null,null],
    ['Finance',null,null],
    ['Information Technology',null,null],
    ['Program Services',null,null],
    ['Program Support',null,null],
    ['Provider Resources',null,null],
    ['MOST',null,null],
    ['Family Resources',null,null],
    ['Family Resources - Chatham Site',null,null],
    ['Team 13',null,null],
    ['Team 14',null,null],
    ['Enhanced Programs',null,null],
    ['Teen Parent Program',null,null],
    ['External Relations',null,null],
] AND here is is when it is nested the way I want it (I did this by hand): [ ['Organization XYZ',null,null,
    ['Operations',null,null,
        ['Finance',null,null],
        ['Information Technology',null,null]
    ],
    ['Program Services',null,null,
        ['Program Support',null,null]
        ['Provider Resources',null,null,
            ['MOST',null,null]
        ],
        ['Family Resources',null,null,
            ['Family Resources - Chatham Site',null,null,
                ['Team 13',null,null],
                ['Team 14',null,null]
            ],
            ['Enhanced Programs',null,null,
                ['Teen Parent Program',null,null]
            ]
        ],
    ],
    ['External Relations',null,null]
] ] Any help would be great. I am stuck. Thanks, Charles

« previous php.general (#215799) next »