Ted
Ted

Reputation: 35

Php recursive function for treeview

I have an array $tree with this structure :

Array
(
   [0] => Array
        (
            ['fichier'] => 'test.xml'
            ['classes'] => Array
                         (
                             [0] => Array
                                  (
                                     ['Id'] => '10'
                                     ['IdParent'] => 'L'
                                  )
                             [1] => Array
                                  (
                                     ['Id'] => '10-01'
                                     ['IdParent'] => '10'
                                  )
                             ...
                         )
        )
   [1] => Array
        (
            ['fichier'] => 'test2.xml'
            ['classes'] => Array
                         (
                             [0] => Array
                                  (
                                     ['Id'] => '10'
                                     ['IdParent'] => 'L'
                                  )
                             [1] => Array
                                  (
                                     ['Id'] => '10-02'
                                     ['IdParent'] => '10'
                                  )
                             ...
                         )
        )
        )
   [2] => Array
        (
           ...
        )
   ...
)

So structure like

L              L            ...
10             10
10-01          10-02
10-01-01       10-02-02
10-01-02       10-02-03

And i try to make a treeview like :

L
 -> 10
     -> 10-01
        -> 10-01-01
        -> 10-01-02
     -> 10-02
        -> 10-02-02
        -> 10-02-03

i wrote a recursive function :

function treeview($tree, $indiceTree, &$prec, $id = 'ICM'){                 
                $indice = 0;
                for ($i = ($indiceTree-sizeof($tree)); $i < sizeof($tree); $i++) {
                    for ($j = 0; $j < sizeof($tree[$i]['classes']); $j++) {                     
                        if($tree[$i]['classes'][$j]['IdParent']==$id) {
                            if (strpos($prec,$tree[$i]['classes'][$j]['Id']) == false) {                                
                                echo $tree[$i]['classes'][$j]['Id']; echo "<br><br>";                               
                                $prec .= ";".$tree[$i]['classes'][$j]['Id'];
                                break;                              
                            }                                           
                            treeview($tree, $indiceTree+$indice,$prec, $tree[$i]['classes'][$j]['Id']);                         
                            break;
                        }
                    }
                    $indice++;                  
                }                               
            }

The problem is this function never stop. I mean the display result is good but it displayed only when the PHP time out come : Fatal error: Maximum execution time of 60 seconds exceeded

the initial array ($tree) has 142 elements so the execution is really too long to be used in a web app. Function seems to never stop when finish the array. If you can help me with this function. Thank you in advance

Upvotes: 0

Views: 1531

Answers (1)

Akif Hussain Sayyed
Akif Hussain Sayyed

Reputation: 596

I have a function that will be helpful for you hopefully

function create_array($number, $data) {
    global $aFinal;

    $result = array();
//    fPrintR($data);
    foreach ($data as $row) {
        if ($row['ParentID'] == $number) {
            $result["put the value you want to show in the tree"] = create_array($row['SubID'], $data);
        }
    }
    return $result; //$result;
}

the below function is of data which you are getting from DB, it returns an array

$aAdmnTrees = fGetSubscribers();

Here call create_array function and pass the data array

$aOppsArray = create_array(here you have to put ID of super Parent, $aAdmnTrees);

This function is to display HTML of tree

function recArray($aOppsArray) {
        ?>
        <ul>
            <?php
            foreach ($aOppsArray as $data => $value) {
                if (!empty($data)) {
                    ?>
                    <ul style="list-style-type: none; background-color: #336699; padding: 10px; width: 300px;">
                        <li style=" color: #FFFFFF;"><?php echo $data; ?></li>
                    </ul>
                    <?php
                }
                if (is_array($value)) {
                    recArray($value);
                } else {
                    echo $value . " ";
                }
            }
            ?>
        </ul>
        <?php
    }

Call to the upper last function

recArray($aOppsArray);

Upvotes: 1

Related Questions