--- db/prgsrc/dbxml.php 2008/02/08 22:49:12 1.1 +++ db/prgsrc/dbxml.php 2008/02/09 10:40:59 1.2 @@ -10,57 +10,93 @@ mysql_query ("SET NAMES 'utf8'"); $tour = $_GET['tour']; -if (preg_match('/\./',$tour)) -{ - $temp = explode('.', $tour); - $fname=$temp[0];$n=$temp[1]; - - - $sth = mysql_query( - $sql="SELECT t2.Id FROM Tournaments as t1, - Tournaments as t2 - WHERE (t1.FileName = '$fname.txt' OR t1.FileName='$fname') - AND t1.Id=t2.ParentId AND t2.Number=$n") or die(mysql_error().": $sql"); -} -else -{ - $sth=mysql_query($sql="SELECT Id FROM Tournaments WHERE FileName = '$tour.txt' OR FileName = '$tour'") or die (mysql_error().": $sql"); -} - -$res = mysql_fetch_row ($sth); -$id=$res[0]; - -$sth = mysql_query("SELECT * FROM Tournaments WHERE Id=$id") or die(mysql_error()); -$tournament = mysql_fetch_assoc($sth); $xw = new xmlWriter(); $xw->openMemory(); $xw->startDocument('1.0','utf8'); $xw->startElement('tournament'); +$tournament = getTournament($tour); + foreach ($tournament as $key => $value) { $xw->writeElement ($key, $value); } -$sth = mysql_query("SELECT * FROM Questions WHERE ParentId =$id"); +printTours($tournament[Id]); + +printQuestions($tournament[Id]); + +$xw->endElement('tournament'); + +$xmlResult = $xw->outputMemory(true); +$xmlResult = preg_replace('/\>\s*\\n<", $xmlResult); + +header("Content-Type: text/xml"); + +print $xmlResult; + +function printTours($tourId) { + global $xw; + $sth = mysql_query("SELECT * FROM Tournaments WHERE ParentId =".$tourId); + + while ($tour = mysql_fetch_assoc($sth) ) + { + $xw->startElement('tour'); + foreach ($tour as $key => $value) + { + $xw->writeElement($key, $value); + } + $xw->endElement('tour'); + } + +} + -while ($question = mysql_fetch_assoc($sth) ) +function printQuestions ($tourId) { + global $xw; + $sth = mysql_query("SELECT * FROM Questions WHERE ParentId =".$tourId); + + while ($question = mysql_fetch_assoc($sth) ) + { $xw->startElement('question'); foreach ($question as $key => $value) { $xw->writeElement($key, $value); } $xw->endElement('question'); + } + } -$xw->endElement('tournament'); -$xmlResult = $xw->outputMemory(true); -$xmlResult = preg_replace('/\>\s*\\n<", $xmlResult); -header("Content-Type: text/xml"); +function getTournament($tour) { + $id = textId2Id($tour); + + $sth = mysql_query("SELECT * FROM Tournaments WHERE Id=$id") or die(mysql_error()); + $tournament = mysql_fetch_assoc($sth); + return $tournament; +} -print $xmlResult; +function textId2Id($tour) { + if (preg_match('/\./',$tour)) + { + $temp = explode('.', $tour); + $fname=$temp[0];$n=$temp[1]; + $sth = mysql_query( + $sql="SELECT t2.Id FROM Tournaments as t1, + Tournaments as t2 + WHERE (t1.FileName = '$fname.txt' OR t1.FileName='$fname') + AND t1.Id=t2.ParentId AND t2.Number=$n") or die(mysql_error().": $sql"); + } + else + { + $sth=mysql_query($sql="SELECT Id FROM Tournaments WHERE FileName = '$tour.txt' OR FileName = '$tour'") or die (mysql_error().": $sql"); + } + $res = mysql_fetch_row ($sth); + $id=$res[0]; + return $id; +} ?> \ No newline at end of file