Page 1 of 1

Recursion, doing my head in...

Posted: Wed Dec 06, 2006 4:07 pm
by amir
Everah | Editing var_dump data to be more readable.

Ok, I have a dynamically generated data structure that could look something like the the below... basically a tree structure, left to right....

Code: Select all

Array
(
    [0] => Array
        (
            [content] => a
            [children] => Array
                (
                    [0] => Array
                        (
                            [content] => b
                            [children] =>
                        )

                    [1] => Array
                        (
                            [content] => c
                            [children] =>
                        )

                    [2] => Array
                        (
                            [content] => d
                            [children] =>
                        )

                )

        )
    [1] => Array
        (
            [content] => e
            [children] => Array
                (
                    [0] => Array
                        (
                            [content] => f
                            [children] =>
                        )
                    [1] => Array
                        (
                            [content] => g
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [content] => i
                                            [children] =>
                                        )
                                    [1] => Array
                                        (
                                            [content] => j
                                            [children] =>
                                        )
                                    [2] => Array
                                        (
                                            [content] => k
                                            [children] =>
                                        )
                                    [3] => Array
                                        (
                                            [content] => l
                                            [children] =>
                                        )
                                )
                        )
                    [2] => Array
                        (
                            [content] => h
                            [children] =>
                        )
                )
        )
)
I want to display these from left to right, top to bottom, with peer nodes underneath each other....

Here is some half finished code, basically, the X value is correct, I just need to get the Y value...

Code: Select all

<?
class FlowReport
{
    var $x;
    var $y;
   
    var $box_width;
    var $box_height;
    var $box_spacing_x;
    var $box_spacing_y;
   
    function FlowReport ( $data )
    {
        $this->data          = $data;
       
        $this->box_width        = 190;
        $this->box_height       = 60;
        $this->box_spacing_x    = 50;
        $this->box_spacing_y    = 50;
         
    }
       
    function set_start_xy( $x, $y )
    {
        $this->x                = $x;
        $this->y                = $y;  
    }
   
    function render( $dataset = "", $depth = '' )
    {
        //first time in the function, assign the root data
        if ( !is_array ( $dataset ) )
        {
            $depth = 0;
            $dataset = $this->data;
        }
       
       
        //for every element in the root...
        for ( $y = 0; $y < count ( $dataset ); $y++ )
        {
           
            //before we plot the next node, plot the
            //children of the current node.
            if ( is_array ( $dataset[ $y ]['children'] ) )
            {
                $this->render ( $dataset[ $y ]['children'], $depth + 1 );                
            }

            //x is always the depth*padding.
            $xplot = $depth * ($this->box_width + $this->box_spacing_x);
           
            //WHERE TO DO I FIND Y?
           
            $this->render_cell ( $xplot, $yplot, $dataset[ $y ]['content'], "" );
        }
    }
   
    function render_cell ( $offset_x, $offset_y, $content, $class )
    {
        $this->div ( $offset_x, $offset_y, $class );
        echo $content;
        $this->end_div();
    }
   
    function end_div()
    {
        ?></div><?
    }
    function div ( $offset_x, $offset_y, $class = "" )
    {
        ?><div style='border: 1px solid black; position: absolute;
                 left: <?=$this->x + $offset_x?>px;
                 top: <?=$this->y + $offset_y?>px;
                 height: <?=$this->box_height?>px;
                 width: <?=$this->box_width?>px;' class='<?=$class?>'><?    
    }    
}
?>
TIA!

Posted: Wed Dec 06, 2006 4:58 pm
by Ollie Saunders
var_export your data and show your desired output please.