Jump to content

Change the look of tree output from recursion


Gotharious

Recommended Posts

Hello all,

I'm trying to change the way the output look like from Recursion

instead of it looking like this

binary-tree-view.jpg

 

and make it look like this

 

clip-image0244-thumb.jpg

 

 

 

here is my code

 

<?php
$user = $_GET['id'];
echo '<hr>'; 
function display_mptt($user) {
global $db;
$id = $_GET['id'];
// retrieve the left and right value of the $root node
$sql2 = "SELECT * from mptt where id= ".$id."";

$result2 = mysql_query($sql2 ,$db);
if(!$row2 = mysql_fetch_array($result2)) echo mysql_error();
echo '<h1>Your Tree</h1>';

// start with an empty $right stack
$right = array();

// now, retrieve all descendants of the $root node
  $sql = "SELECT * from mptt WHERE `left` BETWEEN ".$row2['left']." AND ".$row2['right']." ORDER BY 'left' ASC";
$result = mysql_query($sql ,$db);

// display each row
  while ($row = mysql_fetch_array($result)) {
  // only check stack if there is one
  if (count($right)>0) {
  // check if we should remove a node from the stack
  while ($right[count($right)-1]<$row['right']) {
  array_pop($right);
  }
  }
// display indented node title

// add this node to the stack
  $right[] = $row['right'];
  }
  echo str_repeat('      ',count($right)).$row['title']."<br>"; 
} 
  
  display_mptt(1);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.