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
https://forums.phpfreaks.com/topic/80494-loop-to-create-tree-menu/
Share on other sites

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.