File:  [Local Repository] / db / prgsrc / dbxml.php
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Tue Feb 23 10:18:01 2010 UTC (14 years, 1 month ago) by roma7
Branches: MAIN
CVS tags: HEAD
xml export bug is fixed

<?php
$hostname   = "localhost";
$database   = "chgk";
$username   = "piataev";
$password   = "";

$link = mysql_connect($hostname.':'.$port, $username, $password) or die("Can't connect to database");
mysql_select_db($database,$link) or die ("Can't select to database");
mysql_query ("SET NAMES 'utf8'");
$tour = $_GET['tour'];


$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);
}

printTours($tournament[Id]);

printQuestions($tournament[Id]);

$xw->endElement(); 

$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();
    }

}


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();
    }

}



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;
}

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;
}

?>

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>