PHP Classes

File: navtree2_demo.php

Recommend this page to a friend!
  Classes of Mark Quah   NavTree   navtree2_demo.php   Download  
File: navtree2_demo.php
Role: Example script
Content type: text/plain
Description: NAVTREE2 Demo
Class: NavTree
Explorer-like tree
Author: By
Last change: NavTree2 Demo generate a framed page

Left Frame generate an explorer like directory listing of the web document root. Clicking on the directory will show the content on the right frame.

Need navtree2.php, navtree2.js and images. Need to find yourself icons for root.gif and dir.gif.
Date: 20 years ago
Size: 6,506 bytes
 

Contents

Class file image Download
<?php

    
//#-----------------------------------------------------------------
    //#---- 4 HTML page in one
    //#---- 1. To display header
    //#---- 2. To display directory listing
    //#---- 3. To display welcome page
    //#---- 4. To display footer
    //#---- Determine by <webpage>?page=<content> link
    //#-----------------------------------------------------------------
   
if ( isset( $_GET['page'] ) )
    { switch (
$_GET['page'])
        {
            case
'header':
                echo
"<BODY BGCOLOR=lightyellow>";
                echo
"<BASE TARGET=_parent>";
                echo
"<H1>This is the header</H1>";
                echo
"</BODY>";
                break;
            case
'footer':
                echo
"<BODY BGCOLOR=lightyellow>";
                echo
"<BASE TARGET=_parent>";
                echo
"<H1>This is the footer</H1>";
                echo
"</BODY>";
                break;
            case
'directory':
               
PageDirectory();
                break;
            case
'main':
                echo
"<H1>Hello</H1>";
                break;
        }
        exit;
    }
    else
    {
PageFrameSet();
        exit;
    }


   
//#-----------------------------------------------------------------
    //#---- FUNCTION :: PageFrameSet()
    //#---- DESCRIPTION ::
    //#---- Display Frame Set HTML Code
    //#-----------------------------------------------------------------
   
function PageFrameSet()
    {
        global
$_SERVER;
       
$self_name = $_SERVER['PHP_SELF'];
?>

<HTML>
<HEAD>
<TITLE>Web Information</TITLE>
</HEAD>
<frameset rows="90, *, 90">
      <frame name="herader"
             target="_top"
             src="<?php echo $self_name; ?>?page=header"
             frameborder=0
             noresize
             scrolling="none">
      <frameset cols="30%,*">
            <frame name="contents"
                   target="main"
                    src="<?php echo $self_name; ?>?page=directory"
                    frameborder=0
                    scrolling="auto">
            <frame name="main"
                   src="<?php echo $self_name; ?>?page=main"
                   target="main"
                   frameborder=0
                   scrolling="auto">
      </frameset>
      <frame name="footer"
             target="main"
             src="<?php echo $self_name; ?>?page=footer"
             frameborder=0
             noresize
             scrolling="none">
      <noframes>
          <body>
          <p>This page uses frames, but your browser doesn't support them.</p>
          </body>
      </noframes>
</frameset>
<?php
       
exit;
    }
// end PageFrameSet


    //#-----------------------------------------------------------------
    //#---- FUNCTION :: GetFileList($p_dir, $parent_id)
    //#---- DESCRIPTION ::
    //#---- Read all the file under $p_dir
    //#---- The root node id will be the $parent_id
    //#---- INPUT ::
    //#---- p_dir : root of directory
    //#---- Parent_id : node number that contain the tree
    //#---- OUTPUT ::
    //#---- none
    //#---- OTHERS ::
    //#-----------------------------------------------------------------
   
function GetFileList($p_dir="", $parent_id=0, $parent_path="/")
    {
        global
$navtree;
        global
$file_path;
       
$dir = $p_dir;
       
// Open a known directory, and proceed to read its contents
       
$dh = ( is_dir($dir) ? opendir($dir): FALSE);
        if (
$dh)
        { while ( (
$file = readdir($dh)) !== false )
            {
$ftype = filetype("$dir/$file");
                if (
$file != "." && $file != ".." )
                {
//----- Add a node
                    // change aafile to file if you want to show file as well
                   
if ( $ftype == "dir" || $ftype == "aafile" )
                    {
                       
$node_id = $navtree->AddNode($file, $ftype, $parent_id, 0);
                       
$file_path[$node_id] = "$parent_path/$file";
                }
                   
//echo "Adding node $node_id : $file <BR>";
                   
if ($ftype == "dir")
                    {
                          
GetFileList("$dir/$file", $node_id, "$parent_path/$file" );
                    }
                }
// if file = "."
           
} // end while
           
closedir($dh);
        }
// if opendir
   
} // end GetFileList

    //#-----------------------------------------------------------------
    //#---- FUNCTION :: Transform()
    //#---- Transform the plain content to href
    //#-----------------------------------------------------------------
   
function Transform()
    { global
$navtree;
        global
$file_path;
        for (
$i = 0; $i <= $navtree->no_node; $i ++)
        {
$content = $navtree->node_content[$i];
           
$ftype = $navtree->node_class[$i];
           
// stuff an icon in front and a href behind
           
$content = "<IMG SRC='IMAGE/$ftype.gif'></IMG>".
                       
"<A TARGET='main' HREF='".$file_path[$i]."'>$content</A>";
           
$navtree->node_content[$i] = $content;
        }
    }

    function
PageDirectory()
    {
        include_once
"navtree2.php";
        global
$navtree;
        global
$_SERVER;
        global
$file_path;

      
//======= MAIN CODE ===================================================
        //---- Get Data
       
$root_path="http://".$_SERVER['SERVER_NAME'];
       
$file_path[0] = $root_path;
       
$navtree = new NAVTREE2("DIVTREE", "ROOT", "ROOT", 0);
       
GetFileList($_SERVER['DOCUMENT_ROOT'], 0, $root_path);
       
Transform();
       
//----- Set Style - as in the node_class during AddNode
       
echo "<HTML>";
        echo
"<HEAD>";
        echo
"<STYLE>";
        echo
"A, .dir { font: bold 8pt; color: black; background: lightblue;}";
        echo
":hover { background: yellow;}";
        echo
":visited { color: black;}";
        echo
":hover { background: yellow;}";
        echo
".root { font: bold 10pt}";
        echo
"</STYLE>";
        echo
"</HEAD>";
        echo
"<BODY>";
       
//---- Generate a Dynamic Tree
       
echo "<DIV id='DIVTREE' style='width:300; background: lightgreen;'>GenTree</DIV>";
       
$navtree->GenJSTree();
        echo
"</BODY></HTML>";
    }
// End Page Directory
?>