Exam detailid
Exam detailid
ID
Exam aeg
Koht
Eksamineerija nimi
Opilane nimi
Kestvus (tunnid)
Email
1
2025-08-25 14:30
30087 Garrison Street
Jordanna Sherbrook
Emelyne Posselt
2
jordannaaaaaaaaa.sherbrook@example.com
2
2025-07-25 09:15
35451 Schurz Trail
Elli Harwood
Rickard Giabuzzi
1
elli.harwood@example.com
3
2025-05-04 10:45
43665 Fairfield Circle
Julietta Duffett
Arda Shaddick
3
julietta.duffett@example.com
3
2025-05-04 10:50
test test test
test test
testop testop
3
test.test@example.com
4
2025-05-14 12:00
4 Bashford Avenue
Hewett Canty
Nissa Barnwell
1
hewett.canty@example.com
5
2025-01-14 08:00
7 Jackson Way
Pietra Clew
Yorgos Elrick
2
pietra.clew@example.com
6
2025-09-15 11:30
77257 Meadow Valley Parkway
Xenos Kilbourne
Myrvyn Beatens
3
xenos.kilbourne@example.com
7
2025-01-21 07:45
50389 Kim Center
Connie Sutherden
Margarethe Jans
1
connie.sutherden@example.com
8
2025-08-19 13:00
2281 Clarendon Trail
Tiffi Cherrison
Susanne Pariso
4
tiffi.cherrison@example.com
9
2025-05-31 16:00
820 Sunnyside Court
Davon Gronaver
Clotilda Clowley
4
davon.gronaver@example.com
10
2025-05-28 14:00
4067 Briar Crest Junction
Florida Sibson
Marena Vasler
3
florida.sibson@example.com
Näita koodi
Eksamid.php
<?php //XML fail kus on andmed $xml = new DOMDocument ; $xml -> load ( 'XML/eksamid.xml' ); //otsingu ja filtri andmed vormist $searchName = isset( $_POST [ 'name' ]) ? $_POST [ 'name' ] : '' ; $filterDate = isset( $_POST [ 'date' ]) ? $_POST [ 'date' ] : '' ; // Sorteerimise parameetrid $sortColumn = isset( $_GET [ 'sort' ]) ? $_GET [ 'sort' ] : 'id' ; $sortOrder = isset( $_GET [ 'order' ]) ? $_GET [ 'order' ] : 'asc' ; //filtreeritud XML $filteredXml = new DOMDocument ; $filteredXml -> appendChild ( $filteredXml -> createElement ( 'eksamid' )); foreach ( $xml -> getElementsByTagName ( 'eksam' ) as $eksam ) { //nimed ja aeg $examinerNode = $eksam -> getElementsByTagName ( 'eksamineerijanimi' )-> item ( 0 ); $studentNode = $eksam -> getElementsByTagName ( 'opilanenimi' )-> item ( 0 ); $dateNode = $eksam -> getElementsByTagName ( 'eksamiaeg' )-> item ( 0 ); //kui element puudub, siis on tuhjaks $examinerName = $examinerNode ? $examinerNode -> nodeValue : '' ; $studentName = $studentNode ? $studentNode -> nodeValue : '' ; $examDate = $dateNode ? $dateNode -> nodeValue : '' ; //kas nimi ja kuupaev on oige if ((empty( $searchName ) || stripos ( $examinerName , $searchName ) !== false || stripos ( $studentName , $searchName ) !== false ) && (empty( $filterDate ) || strpos ( $examDate , $filterDate ) !== false )) { $filteredXml -> documentElement -> appendChild ( $filteredXml -> importNode ( $eksam , true )); } } //filtreeritud XML $xpath = new DOMXPath ( $filteredXml ); $sortedXml = new DOMDocument ; $sortedXml -> appendChild ( $sortedXml -> createElement ( 'eksamid' )); $nodes = $xpath -> query ( '/eksamid/eksam' ); $sortedArray = []; foreach ( $nodes as $node ) { $sortedArray [] = $node ; } // Sorteerin massiivi usort ( $sortedArray , function ( $a , $b ) use ( $sortColumn , $sortOrder ) { $aNode = $a -> getElementsByTagName ( $sortColumn )-> item ( 0 ); $bNode = $b -> getElementsByTagName ( $sortColumn )-> item ( 0 ); $aValue = $aNode ? $aNode -> nodeValue : '' ; $bValue = $bNode ? $bNode -> nodeValue : '' ; if ( $aValue == $bValue ) { return 0 ; // Equal } if ( $sortOrder === 'asc' ) { return ( $aValue < $bValue ) ? - 1 : 1 ; } else { return ( $aValue > $bValue ) ? - 1 : 1 ; } }); // Lisan sorteeritud eksamid XML foreach ( $sortedArray as $node ) { $sortedXml -> documentElement -> appendChild ( $sortedXml -> importNode ( $node , true )); } // XSLT $xslt = new DOMDocument ; $xslt -> load ( 'XML/eksamid.xslt' ); $proc = new XSLTProcessor ; $proc -> importStylesheet ( $xslt ); $html = $proc -> transformToXML ( $sortedXml ); header ( 'Content-Type: text/html; charset=UTF-8' ); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Exam Details</title> <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> <script> $(function() { $("#date").datepicker({ dateFormat: 'yy-mm-dd' }); }); </script> <script> $(document).ready(function(){ $(".codeshowbut").click(function(){ $(".codeshow").slideToggle("slow"); }); }); </script> <style> .codeshowbut{ bgcolor="Red" } </style> </head> <body> <h1>Exam detailid</h1> <form method="POST"> <div> <label for="name">Otsi nime järgi:</label> <input type="text" id="name" name="name" value="<?php echo htmlspecialchars ( $searchName ); ?> " placeholder="Sisestage nimi..."> <button type="submit">Otsi</button> </div> <div style="margin-top: 1%"> <label for="date">Filtreeri kuupäeva järgi:</label> <input type="text" id="date" name="date" value="<?php echo htmlspecialchars ( $filterDate ); ?> " placeholder="Valige kuupäev..."> <button type="submit">Otsi</button> </div> </form> <div> <table> <thead> </thead> <tbody> <?php if ( $html ) { echo $html ; } else { echo "<p>Tulemusi ei leitud.</p>" ; } ?> </tbody> </table> </div> <button class="codeshowbut">Näita koodi</button> <pre class="codeshow" class="brush: php" style="display:none"> <h1>Eksamid.php</h1><?php highlight_string ( file_get_contents ( __FILE__ )); ?> </pre> <pre class="codeshow" class="brush: php" style="display:none"> <h1>ExportToJSON.php</h1><?php highlight_string ( file_get_contents ( "ExportToJSON.php" )); ?> </pre> <pre class="codeshow" class="brush: php" style="display:none"> <h1>ImportToXml.php</h1><?php highlight_string ( file_get_contents ( "ImportToXml.php" )); ?> </pre> </body> </html>
ExportToJSON.php
<?php $xmlFile = 'XML/eksamid.xml' ; //kas sealon XML? if (! file_exists ( $xmlFile )) { die( "XML-faili ei leitud!" ); } $xml = new DOMDocument (); $xml -> load ( $xmlFile ); //teisendamiseks massiiviks function xmlToArray ( $xmlNode ) { //salvestan $result = []; //vaatan attr if ( $xmlNode -> hasAttributes ()) { foreach ( $xmlNode -> attributes as $attr ) { $result [ '@attributes' ][ $attr -> name ] = $attr -> value ; } } /* Käin läbi kõik XML elemendi alamnode'id. Kui node on tekst, salvestan selle väärtuse. Kui node on XML element, töötlen seda rekursiivselt. Kui sama nimega elemente on mitu, panen need massiivi. Nii saan XML struktuuri muuta PHP massiiviks. */ foreach ( $xmlNode -> childNodes as $node ) { if ( $node -> nodeType == XML_TEXT_NODE ) { $text = trim ( $node -> textContent ); if ( $text !== '' ) { if (empty( $result )) { return $text ; } $result [ '@value' ] = $text ; } } elseif ( $node -> nodeType == XML_ELEMENT_NODE ) { $child = xmlToArray ( $node ); $nodeName = $node -> nodeName ; if (isset( $result [ $nodeName ])) { if (! is_array ( $result [ $nodeName ]) || ! array_key_exists ( 0 , $result [ $nodeName ])) { $result [ $nodeName ] = [ $result [ $nodeName ]]; } $result [ $nodeName ][] = $child ; } else { $result [ $nodeName ] = $child ; } } } return $result ; } //XMl -> PHP massiv $xmlArray = xmlToArray ( $xml -> documentElement ); //massiiv -> JSON $jsonData = json_encode ( $xmlArray , JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ); $jsonFile = 'eksamid.json' ; file_put_contents ( $jsonFile , $jsonData ); // Kontrollin, kas JSON fail loodi if ( file_exists ( $jsonFile )) { // Saadan faili alla laadimiseks header ( 'Content-Description: File Transfer' ); header ( 'Content-Type: application/json' ); header ( 'Content-Disposition: attachment; filename="' . basename ( $jsonFile ) . '"' ); header ( 'Expires: 0' ); header ( 'Cache-Control: must-revalidate' ); header ( 'Pragma: public' ); header ( 'Content-Length: ' . filesize ( $jsonFile )); readfile ( $jsonFile ); exit; } else { echo "JSON-faili ei leitud." ; }
ImportToXml.php
<?php $message = "" ; //kas vorm saadeti ja fail on olemas if ( $_SERVER [ 'REQUEST_METHOD' ] === 'POST' && isset( $_FILES [ 'jsonFile' ])) { //kas faili üleslaadimine õnnestus if ( $_FILES [ 'jsonFile' ][ 'error' ] === UPLOAD_ERR_OK ) { $jsonContent = file_get_contents ( $_FILES [ 'jsonFile' ][ 'tmp_name' ]); //JSON -> PHP massiiv $arrayData = json_decode ( $jsonContent , true ); if ( json_last_error () !== JSON_ERROR_NONE ) { die( "Viga: vale JSON" ); } //PHP massiiv -> XML function arrayToXml ( $data , $rootElement = 'root' , $level = 0 ) { $indent = str_repeat ( " " , $level ); $xml = "" ; // kui on numbrilise indeksiga massiiv if ( is_array ( $data ) && array_keys ( $data ) === range ( 0 , count ( $data ) - 1 )) { foreach ( $data as $subValue ) { $xml .= arrayToXml ( $subValue , $rootElement , $level ); } return $xml ; } // XML atribuutide $attributes = "" ; if ( is_array ( $data ) && isset( $data [ '@attributes' ])) { foreach ( $data [ '@attributes' ] as $attrKey => $attrValue ) { $attributes .= " $attrKey =\"" . htmlspecialchars ( $attrValue ) . "\"" ; } unset( $data [ '@attributes' ]); } //kas andmed massiivis if ( is_array ( $data )) { $xml .= " $indent < $rootElement$attributes >\n" ; foreach ( $data as $key => $value ) { $xml .= arrayToXml ( $value , $key , $level + 1 ); } $xml .= " $indent </ $rootElement >\n" ; } else { $xml .= " $indent < $rootElement$attributes >" . htmlspecialchars ( $data ) . "</ $rootElement >\n" ; } return $xml ; } $xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<eksamid>\n" ; if (isset( $arrayData [ 'eksam' ])) { $xmlData .= arrayToXml ( $arrayData [ 'eksam' ], 'eksam' , 1 ); } $xmlData .= "</eksamid>\n" ; //salvestan XML faili file_put_contents ( __DIR__ . "/XML/eksamid.xml" , $xmlData ); // edukas teade kasutajale $message = "Fail <b>eksamid.xml</b> edukalt uuendatud!" ; } else { //kui faili üleslaadimine ebaõnnestus $message = "Faili üleslaadimise viga!" ; } } ?> <!DOCTYPE html> <html lang="ru"> <head> <meta charset="UTF-8"> <title>JSON -> XML</title> <style> body { font-family: Arial, sans-serif; background-color: #f9f9f9; color: #333; margin: 0; padding: 20px; } h1 { text-align: center; color: #4CAF50; } .export-button { display: block; margin: 20px auto; padding: 12px 24px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); transition: background-color 0.3s ease; } .export-button:hover { background-color: #45a049; } form { text-align: center; margin-bottom: 20px; } .back-button { display: block; margin: 20px auto; padding: 12px 24px; background-color: #B3B3B3; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); transition: background-color 0.3s ease; } .back-button:hover { background-color: #A8A8A8; } .message { text-align: center; font-weight: bold; color: green; } </style> </head> <body> <h1>XML teisendamiseks laadi üles JSON</h1><?php if (!empty( $message )): ?> <p class="message"><?= $message ?> </p><?php endif; ?> <form method="POST" enctype="multipart/form-data"> <input class="export-button" type="file" name="jsonFile" accept="application/json" required> <button class="export-button" type="submit">Teisenda</button> </form> <button class="back-button" onclick="history.back()">Tagasi</button> </body> </html>