Hi,

Below is a sample of how I store and display in PHP tree structure stored in
the database.
Can anybody tell me how  to rewrite this code into PHPTAL?
I assume, that I should use macro recurrency, but how to prepare array in
such case?

TIA


<?php

// view with fixed width font

/*
               sql
             /     \
    postgresql      oracle-----__
        |         /      |           \
     linux     solaris  linux      windows
                     /       \
                  glibc1   glibc2
*/

$list = array(
   // $items
   // array([name]            [order]    [path]
[depth])
      array('sql',            '1',        '/000001/',
0),
      array('postgresql',    '1',        '/000001/ 000001/',
1),
      array('linux',        '9',        '/000001/ 000001/ 000009/',
2),
      array('oracle',        '2',        '/000001/ 000002/',
1),
      array('solaris',        '3',        '/000001/ 000002/ 000003/',
2),
      array('linux',        '5',        '/000001/ 000002/ 000005/',
2),
      array('glibc1',        '6',        '/000001/ 000002/ 000005/ 000006/',
3),
      array('glibc2',        '7',        '/000001/ 000002/ 000005/ 000007/',
3),
      array('windows',        '100',        '/000001/ 000002/ 000100/',
    2)
);

// display tree via php

// initial depth is one lower than lowest possible
// to start from opening new <ul> sublist
$prevDepth = -1;

foreach ($list as $item) {
    // values from array
    $depth = $item[3];
    $name = $item[0];

    // difference between current and previous depth
    $depthDifference = $depth - $prevDepth;

    if ($depthDifference > 0) {
        // open new sublist
        echo '<ul>';
    } elseif ($depthDifference == 0) {
        // no new sublists
        // close current item
        echo '</li>';
    } elseif ($depthDifference < 0) {
        // there are $depthDifference not closed sublists
        // close them all
        for ($j = $depthDifference; $j < 0; $j++) {
            echo '</li></ul></li>';
        }
    }

    // display current item
    echo "<li>$name";

    // remember current depth for next loop
    $prevDepth = $depth;
}

// there are still $depth not closed sublists
// close them all
for ($j = 0; $j <= $depth; $j++) {
    echo '</li></ul>';
}

?>
_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal

Reply via email to