<?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);
?>
|