Login   Register  
PHP Classes
elePHPant
Icontem

File: TBasicDOMTest.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Jonas Raoni Soares Silva  >  3-Level DOM Implementation  >  TBasicDOMTest.php  >  Download  
File: TBasicDOMTest.php
Role: Example script
Content type: text/plain
Description: A sample file using the TBasicDOM class to manage some items in a tree
Class: 3-Level DOM Implementation
Class to manage data upon the DOM specification.
Author: By
Last change: name change...
Date: 11 years ago
Size: 1,882 bytes
 

Contents

Class file image Download
<?PHP
Require_Once('TBasicDOM.php');
/*Suppose that you have a table with this structure:
ProductsTree (
    Cod    AUTO_INCREMENT,
    Name    String,
    Parent    Integer Default 0;
)
*/

$LastIndent = -1;
$Indent 0;

$Lines = array();
$Lines[] = 'Web Development';
$Lines[] = "\tLanguages";
$Lines[] = "\t\tPHP";
$Lines[] = "\t\tJSP";
$Lines[] = "\tSoftwares";
$Lines[] = "\t\tMacromedia";
$Lines[] = "\t\tPHPEd";
$Lines[] = "Desktop Development";
$Lines[] = "\tLanguages";
$Lines[] = "\t\tDelphi/Kylix";
$Lines[] = "\t\tJBuilder";
$Lines[] = "\tTools";
$Lines[] = "\t\tEnterprise Architect";

$Root = new TBasicDOM;
$Current = &$Root;
ForEach(
$Lines As $Line) {
    
$Indent PREG_Match_All('/\t/'$Line$Matches);
               If(
$Indent $LastIndent$Current = &$Current->AddNode(Trim($Line));
    ElseIf (
$Indent $LastIndent) {
        
$Current=&$Current->GetAncestorAt(($LastIndent $Indent)+1);
        
$Current=&$Current->AddNode(Trim($Line));
    }
    ElseIf (
$Indent == $LastIndent$Current = &$Current->Parent->AddNode(Trim($Line));
    
$LastIndent $Indent;
}

$Node=&$Root->ChildNodes[0]->ChildNodes[0]->Parent->ChildNodes[0]->ChildNodes[0]->Parent->ChildNodes[0]->NextNode();
echo 
$Node->Data,'<BR><BR>';

$Node->Data 'JAVA SERVER PAGE';

$InsertCount 0;

function 
MakeInsert($Name$Parent) {
    Global 
$InsertCount;
    echo 
$Insert 'Insert Into ProductsTree (Name, Parent) Values (\''.$Name.'\','.$Parent.')'.'<BR>';
    ;
    echo 
'Primary Key Record Cod='.(int)$InsertCount++,'<BR><BR>';
}

Function 
MySQLLastInsertId(){
    return 
$GLOBALS['InsertCount'];
}

Function 
ShowDetails($RootNode,$Cod){
    If(
$RootNode->HasChildNodes())
             ForEach(
$RootNode->ChildNodes As $Node) {
                    
MakeInsert($Node->Data,$Cod);
                         
ShowDetails($Node,MySQLLastInsertID());
                 }
}

ShowDetails($Root,0);
?>