Jump to content

loop to create tree menu


shocker-z

Recommended Posts

Hi all,

I've cerated the following code to loop through folders and list all the PDF files and allow a tree diagram of the folder structure and files within the folders.

 

The problem is that after a folder at level 1 is displayed the sub folder has a blank $prvfolders value, but i cant figure out what is setting $prvfolders to null.

 

See code

 

<script type="text/javascript" src="simpletreemenu.js">

/***********************************************
* Simple Tree Menu- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

</script>

<link rel="stylesheet" type="text/css" href="simpletree.css" />
<a href="javascript:ddtreemenu.flatten('treemenu1', 'expand')">Expand All</a> | <a href="javascript:ddtreemenu.flatten('treemenu1', 'contact')">Contact All</a>
<?php
$path = 'E:/liam/resourcedisks/disks/test/pdf/';
echo '<ul id="treemenu1" class="treeview">'."\n";
function findfilesandfolders($path) {
$foldersarray=array();
  $dir_handle = @opendir($path) or die("Unable to open $path");
  $dirname = end(explode("/", $path));
  while (false !== ($file = readdir($dir_handle))) {
  global $foldersarray;
    if ($file!="." && $file!="..") {
      if (is_dir($path."/".$file)) {
  	$folders=count(explode('/',$path.'/'.$file))-7;
	if (($folders < $prvfolders) && (isset($prvfolders))) {
	  echo '</ul>'."\n";
	  echo '</ul>'."\n";
	  echo '<ul id="'.$file.'" class="treeview"><span style="color:#0000FF;">'.$file.' ('.$folders.')</span>'."\n";
	  $prvfolders=$folders;
	} elseif ($folders == $prvfolders) {
	  echo '</ul>'."\n";
	  echo '<ul id="'.$file.'" class="treeview"><span style="color:#0000FF;">'.$file.' ('.$folders.')</span>'."\n";
	  $prvfolders=$folders;
	} else {
	  echo '<ul id="'.$file.'" class="treeview"><span style="color:#0000FF;">'.$file.' ('.$folders.')</span>'."\n";
	  $prvfolders=$folders;
	}
	$foldersarray[]=$file;
	$currentfolder=$file;
        findfilesandfolders($path.'/'.$file);
      
  } else {
    list($filename,$ext)=explode('.',$file);
    if ($ext == 'pdf') {
      echo '<li>'.$file.'</li>'."\n";
    }
  }
    }
  }
  closedir($dir_handle);
}
  echo '</ul>';
findfilesandfolders($path);

?>

<a href="javascript:ddtreemenu.flatten('treemenu2', 'expand')">Expand All</a> | <a href="javascript:ddtreemenu.flatten('treemenu2', 'contact')">Contact All</a>


<script type="text/javascript">
//ddtreemenu.createTree(treeid, enablepersist, opt_persist_in_days (default is 1))

ddtreemenu.createTree("treemenu1", false)
<?php
foreach($foldersarray as $folder) {
  echo 'ddtreemenu.createTree("'.$folder.'", false)'."\n";
}
?>
</script>

 

 

here si the output

 

menu 1 (1/)

sub a (2/)

pdf a1.pdf

pdf a2.pdf

sub b (2/2)

pdf b1.pdf

pdf b2.pdf

sub c (2/2)

pdf c1.pdf

pdf c2.pdf

menu 2 (1/1)

sub a (2/)

pdf a1.pdf

pdf a2.pdf

sub b (2/2)

pdf b1.pdf

pdf b2.pdf

sub c (2/2)

pdf c1.pdf

pdf c2.pdf

menu 3 (1/1)

sub a (2/)

pdf a1.pdf

pdf a2.pdf

sub b (2/2)

pdf b1.pdf

pdf b2.pdf

sub c (2/2)

pdf c1.pdf

pdf c2.pdf

 

inside the brakcets shows the current number of folders and the previouse number of folders ($prvfolders)

 

 

 

Any idea's as im ripping my hair out over this one...

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.