Recursive function and formatting string for javascript tree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux