Jump to content

format tree traversal


GeXus

Recommended Posts

I'm using a tree traversal to store categories.. What I want to do is use the php to format how the categories are displayed..

 

For example, I'm getting the depth of the node, so a result set might look like this

 

name depth

General 1

Games 2

Action 3

Sales 4

Movies 2

Action 3

Drama 3

Sales 4

 

Based on this data, I want to be able to format it such as this..

 

<ul class="tree">
	<li class="closed"><a href="#">Games</a>
	<ul>
		<li><a href="#">Action</a></li>
		<ul>
			<li><a href="#">Sales</a></li>
		</ul>	
	</ul>	
	</li>
	<li class="closed"><a href="#">Movies</a>
	<ul>
		<li><a href="#">Action</a></li>
		<li><a href="#">Drama</a>
                          <ul>
                           <li><a href="#">Sales</a></li>
                         </ul>

                         </li>
	</ul>	
	</li>
</ul>

 

Ultimatly, this will be used for a typical folder tree layout...

 

The problem is how in php do I use the depth value to echo out this format?

 

Thanks a lot!!

 

Link to comment
Share on other sites

If you set up an array of the categories so you have the category and its parent

 

<?php
$cats = array (
        1 => array ('General', 0),        // category, parent
        2 => array ('Games', 1),
        3 => array ('Movies', 1),
        4 => array ('Action', 2),
        5 => array ('Action', 3),
        6 => array ('Sales', 4),
        7 => array ('Drama', 3),
        8 => array ('Sales', 7),
        
);

function listCats (&$cats, $parent)
{
    $k = count($cats);
    for ($id=1; $id<=$k; $id++) {
        $arr = $cats[$id];
        if ($arr[1] == $parent) {
            echo "<ul>\n";
            echo "<li>{$arr[0]}</li>\n";
            listCats($cats, $id);
            echo "</ul>\n";
        }
    }
}

listCats($cats,0);
?>

 

gives -->

[pre]

General

    Games

        Action

              Sales

    Movies

        Action

        Drama

              Sales

[/pre]

 

 

Link to comment
Share on other sites

I don't need glasses, thanks though!

 

This is exactly what I have

id, name, left, right
1, 'ELECTRONICS', 1, 20
2, 'TELEVISIONS', 2, 9
3, 'TUBE', 3, 4
4, 'LCD', 5, 6
5, 'PLASMA', 7, 8
6, 'PORTABLE ELECTRONICS', 10, 19
7, 'MP3 PLAYERS', 11, 14
8, 'FLASH', 12, 13
9, 'CD PLAYERS', 15, 16
10, '2 WAY RADIOS', 17, 18

 

There is nothing more I can give you! There are no dates associated with any of this.

Link to comment
Share on other sites

Bastard!!  Ok.. maybe I do need glasses  :P

 

Anyways, I just figured it out.. was just a matter of using the level and displaying it based on equal, less than or greater then.. much simpler than I had though..

 

Thanks for your help.

Link to comment
Share on other sites

try

 

<?php
$tree = array (
      1 => array (
        'node' => 'ELECTRONICS',
        'left' => 1,
        'right' => 20
        ),
        array (
        'node' => 'TELEVISIONS',
        'left' => 2,
        'right' => 9
        ),
        array (
        'node' => 'TUBE',
        'left' => 3,
        'right' => 4
        ),
        array (
        'node' => 'LCD',
        'left' => 5,
        'right' => 6
        ),
        array (
        'node' => 'PLASMA',
        'left' => 7,
        'right' => 8
        ),
        array (
        'node' => 'PORTABLE ELECTRONICS',
        'left' => 10,
        'right' => 19
        ),
        array (
        'node' => 'MP3 PLAYERS',
        'left' => 11,
        'right' => 14
        ),
        array (
        'node' => 'FLASH',
        'left' => 12,
        'right' => 13
        ),
        array (
        'node' => 'CD PLAYERS',
        'left' => 15,
        'right' => 16
        ),
        array (
        'node' => '2 WAY RADIOS',
        'left' => 17,
        'right' => 18
        )
);

function listNodes (&$tree, $id) {
    if (!isset($tree[$id]['listed'])) {
        echo "<ul>\n";
        
        echo "<li>{$tree[$id]['node']}</li>\n";
        $tree[$id]['listed'] = 1;   // block further listing
        $k = count($tree);
        for ($nid=1; $nid <= $k; $nid++) {
            $nodedata = $tree[$nid];
            if ($nodedata['left'] > $tree[$id]['left'] && $nodedata['right'] < $tree[$id]['right'] )
                listNodes($tree, $nid);
        }
        echo "</ul>\n";
    }
}

listNodes ($tree,1);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.