Skip to content

Instantly share code, notes, and snippets.

@rafi
Forked from shadowhand/tree.php
Created April 15, 2010 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafi/366958 to your computer and use it in GitHub Desktop.
Save rafi/366958 to your computer and use it in GitHub Desktop.
Tree view
<?php
$depth = 0;
echo '<ol class="tree">';
foreach ($children as $i => $child)
{
// Get the last and next nodes
$last = isset($children[$i-1]) ? $children[$i-1] : $children[0];
$next = isset($children[$i+1]) ? $children[$i+1] : $children[0];
if ($child->lvl > $last->lvl)
{
// Increasing depth, start a new list
echo '<ol class="d', $child->lvl, '">';
}
// Start the line
echo "\n", str_repeat("\t", $child->lvl + 1), "<li>";
// Create a link to the user details
$line = html::anchor('users/view/details/'.$child->id, $child->name ? $child->name : $child->username);
if ($child->lvl < 1)
{
// This user is the start of a network
$line = "<strong>{$line}</strong>";
}
// Display the affiliates name and current credit
echo $line;
if ($next->lvl <= $child->lvl)
{
// Next node has the same depth or less, end this line
echo "</li>";
}
if ($next->lvl < $child->lvl)
{
// Calculate the amount of change
$change = $child->lvl - $next->lvl;
while ($change > 0)
{
// Decreasing depth
$change--;
echo "\n", str_repeat("\t", $child->lvl + $change), "</ol></li>";
}
}
}
echo "\n</ol>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment