#!/usr/bin/perl use warnings; use strict; use DBI; use DBD::mysql; use CGI qw( :standard ); my $dbh = DBI->connect( "DBI:mysql:btree", "root", "", { RaiseError => 1 } ); my $string = "CREATE TABLE Nodes ( NID INT NOT NULL AUTO_INCREMENT, PNID INT NOT NULL, DEPTH INT NOT NULL, PRIMARY KEY(NID) )"; $dbh->do( $string ); $dbh->do( q{ INSERT INTO Nodes ( PNID, DEPTH ) VALUES ( 0, 0 ) } ); $dbh->disconnect(); print header(), start_html( "Database Creation" ); print h4( "The btree Database Tables Has Been Created" ); #### #!/usr/bin/perl use warnings; use strict; use DBI; use DBD::mysql; use CGI qw( :standard ); my $dbh = DBI->connect( "DBI:mysql:btree", "root", "", { RaiseError => 1 } ); my ( $sth, $results, $err ); if ( param("Add") ) { my $pnid = param("PNID"); $sth = $dbh->prepare( q{ SELECT DEPTH FROM Nodes WHERE NID = ? } ); $sth->execute($pnid); $results = $sth->fetchrow_arrayref(); warn( $DBI::errstr ) if ( $DBI::err ); if ( $sth->rows == 0 ) { $err = 1; } else { my $query = "INSERT INTO Nodes ( PNID, DEPTH ) VALUES ( " . $pnid . ", " . ( $results->[0] + 1 ) . " )"; $dbh->do( $query ); } $sth->finish(); } #$sth = $dbh->prepare( q{ SELECT * FROM Nodes ORDER BY DEPTH ASC, PNID ASC, NID ASC } ); $sth = $dbh->prepare( q{ SELECT DISTINCT( PNID ), DEPTH FROM Nodes ORDER BY DEPTH ASC } ); $sth->execute(); $results = $sth->fetchall_arrayref(); warn( $DBI::errstr ) if ( $DBI::err ); $sth->finish(); print header(), start_html( "Tree Manager" ); print h3( { -align => "center" }, "Tree Manager" ), hr(), table( { -border => 0, -width => "100%" }, Tr( th( { -align => "left", -colspan => "2" }, "Current Tree:" ) ), Tr( th( { -width => "5%" }, "Depth" ), th( "Tree Nodes" ) ) ); my ( $pnid, $content, $td, $depth, $tman ); for ( @$results ) { if ( $depth != $_->[1] ) { # here we change the tree level $tman .= Tr( td( { -align => "center", -width => "5%" }, $depth ), $td ); $td = ""; } $depth = $_->[1]; $sth = $dbh->prepare( q{ SELECT * FROM Nodes WHERE PNID = ? } ); $sth->execute($_->[0]); $pnid = $sth->fetchall_arrayref(); warn( $DBI::errstr ) if ( $DBI::err ); for ( @$pnid ) { $content .= " [P: $_->[1] ID: $_->[0]] "; } $td .= td( { -align => "center" }, $content ); $content = ""; } $dbh->disconnect(); $sth->finish(); $tman .= Tr( td( { -align => "center", -width => "5%" }, $depth ), $td ); print table( { -border => 1, -width => "100%" }, $tman ); print h5("Check you enetered correctly the parrent node!") if $err; print start_form(), table( { -border => 0, -width => "100%" }, Tr( th( { -align => "left", -width => "10%" }, "Add Node To:" ), td( textfield( -name => "PNID", -size => 10 ) ) ), Tr( td( submit( "Add" ) ), td() ) ), end_form(); print end_html();