views:

58

answers:

1

Hi All, I have all my data in my database. It has following 4 columns

id  source_clust    target_clust    result_clust

1    7       72      649
2    9       572     650
3    649     454     651
4    32      650     435

This data is like tree structure. source_clust and target_clust generate target_clust. target_clust can be source_clust or target_clust to make a new target_clust. Is there any php function or class that I can use to generate tree structure for my data????

I see this MySql site they are doing exactly what I need but I couldn't find how to implement that query in my data.

Thanks !

Edited


Is there any way in java to do it? if we have same data in array ??

+1  A: 

you can download PHP tree (or even binary tree in your case) classes from phpclasses.org, then you can use a script that fills in the tree with the data from your database. You will have more independent parts of the tree, as you have more roots. In the case you wrote you will have 2 roots: 651 and 435.

[...]
$tree = new Tree();
$strsql = "SELECT source_clust, target_clust, result_clust FROM mytable";
$ressql = @mysql_query($strsql);
while(list($v1, $v2, $v3)=@mysql_fetch_array($ressql))
{
    if (!$tree->hasNode($v1))
        $tree->newNode($v1);        
    if (!$tree->hasNode($v2))
        $tree->newNode($v2);        
    if (!$tree->hasNode($v3))
        $tree->newNode($v3);

    $tree->Node($v1)->parent = $tree->Node($v3);
    $tree->Node($v2)->parent = $tree->Node($v3);
}
@mysql_free_result($ressql);
Ervin