Need help to display a tree's content in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djvpx
Forum Newbie
Posts: 4
Joined: Thu Sep 07, 2006 8:11 am

Need help to display a tree's content in php

Post by djvpx »

Hello,
I've made a fonction that stores de content of a group (I call it a zone) in an array.
A group may content:
1 or many other groups (if it's a group, "type"='Z') and/or 1 or many Elements (type='E').
The goal is to display all the elements of a group (and its sub-groups).
Everything works fine except if a group contains more than one group.
Here's my code:

Code: Select all

function TableauElements($groupe) // Receives the name of a group
    { global $tableauE;
    $tableauE = array();
    static $i=0;
    $where = '';
    if (isset($groupe))
    $where = "WHERE ZONENAME = '$groupe'";
    else
    $where = "WHERE ZONENAME = 'RACINE'";
    $sql = 'SELECT * FROM ZONE_ELEMENT '.$where.' ORDER BY NAME_ITEM';
    $result = mysql_query($sql);
    if (!$result)
    {
    return false;
    }
    while ($myrow = mysql_fetch_Array($result)) {
    if ($myrow['TYPE'] == 'Z'){
    TableauElements($myrow['NAME_ITEM']);
    }
    else {
    $tableauE[$i]= $myrow['NAME_ITEM']; //I put the Element's name in the array
    $i++;
    }

    }
    sort ($tableauE);
    return true;
    }

    ?>
Here's my table's content
ID | ZONENAME | NAME_ITEM | TYPE
8 | 3 | 2 | Z
6 | 2 | 1 | Z
79 | 1 | ELEMENT1 | E
83 | 3 | 4 | Z
76 | 4 | ELEMENT2 | E
To read like this:


Group 3 contents group 2
Group 2 contents group 1
Group 1 contents the element ELEMENT1
Group 3 contents group 4
Group 4 contents the element ELEMENT2
This means, that the group 3 contents the group 2 (that contents the group 1) but also the group 4.
When the function is called for group 3, I only receive "element 2" in the array (=the content of group 4).
I also noticed that the function only displays the last sub-group's content when there is more than one sub-group but everything works fine when there is only one.
Can you help me?
Thanks in advance,
Xavier
Post Reply