Solved

Function to make a visual tree like on windows explorer from an array using PHP

Posted on 2003-10-22
3
272 Views
Last Modified: 2006-11-17
Hi

I have a multi-dimensional array of names which I want to display on screen as a windows-explorer style tree.  I set out to make it myself but am having trouble getting there as it is slightly more involved than I expeted !

Is there a script anyone knows of which can do this for me ?

Thanks
Richard
0
Comment
Question by:rfila
3 Comments
 
LVL 11

Expert Comment

by:shmert
ID: 9600085
So, this is something that you would click branches to toggle visible/hidden, yes?  Or is it just a static display of the tree?
0
 
LVL 6

Accepted Solution

by:
aolXFT earned 250 total points
ID: 9600945
<?php

$array = make_array_of_type_described_in_question();

// Method 1:
var_dump($array);

//Method 2:
print_r($array);

//print_r, and var_dump are similar. They format the output differently.

//Method 3

// A custom recusive function to do the job:
// I can't think of how explorer does it at the minute,
// but you should be able to adapt the following.
function show_array($array, $indent = 0, $indent_diff = 2){
        if(is_array($array)){
                foreach($array as $element){
                        show_array($element, $indent + $indent_diff, $indent_diff);
                }
        }else{
                for($i = 0; $i < $indent; ++$i){
                        echo ' ';
                }
                echo $array . "\n";
        }
}

$indent_diff is just a value by how much difference there is between various levels of indenting.
0
 

Expert Comment

by:kplonk
ID: 9664190
I did it using PHP and Javascript, the PHP handels requesting data ofm rthe DB and builds the javascript, the javascript then expands and colapses the tree as needed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now