File:  [Local Repository] / db / prgsrc / dbxml.php
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Sat Feb 9 10:40:59 2008 UTC (16 years, 3 months ago) by roma7
Branches: MAIN
CVS tags: HEAD
Updtate xml

    1: <?php
    2: $hostname   = "localhost";
    3: $database   = "chgk";
    4: $username   = "piataev";
    5: $password   = "";
    6: 
    7: $link = mysql_connect($hostname.':'.$port, $username, $password) or die("Can't connect to database");
    8: mysql_select_db($database,$link) or die ("Can't select to database");
    9: mysql_query ("SET NAMES 'utf8'");
   10: $tour = $_GET['tour'];
   11: 
   12: 
   13: $xw = new xmlWriter();
   14: $xw->openMemory();
   15: $xw->startDocument('1.0','utf8');
   16: $xw->startElement('tournament'); 
   17: $tournament = getTournament($tour);
   18: 
   19: foreach ($tournament as $key => $value)
   20: {
   21: 	$xw->writeElement ($key, $value);
   22: }
   23: 
   24: printTours($tournament[Id]);
   25: 
   26: printQuestions($tournament[Id]);
   27: 
   28: $xw->endElement('tournament'); 
   29: 
   30: $xmlResult = $xw->outputMemory(true);   
   31: $xmlResult = preg_replace('/\>\s*\</', ">\n<", $xmlResult);	
   32: 
   33: header("Content-Type: text/xml");
   34: 
   35: print $xmlResult;	
   36: 
   37: function printTours($tourId) {
   38:     global $xw;
   39:     $sth = mysql_query("SELECT * FROM Tournaments WHERE ParentId =".$tourId);
   40: 
   41:     while ($tour = mysql_fetch_assoc($sth) )
   42:     {
   43: 	$xw->startElement('tour');
   44: 	foreach ($tour as $key => $value)
   45: 	{
   46: 		$xw->writeElement($key, $value);
   47: 	}
   48: 	$xw->endElement('tour');
   49:     }
   50: 
   51: }
   52: 
   53: 
   54: function printQuestions ($tourId)
   55: {
   56:     global $xw;
   57:     $sth = mysql_query("SELECT * FROM Questions WHERE ParentId =".$tourId);
   58: 
   59:     while ($question = mysql_fetch_assoc($sth) )
   60:     {
   61: 	$xw->startElement('question');
   62: 	foreach ($question as $key => $value)
   63: 	{
   64: 		$xw->writeElement($key, $value);
   65: 	}
   66: 	$xw->endElement('question');
   67:     }
   68: 
   69: }
   70: 
   71: 
   72: 
   73: function getTournament($tour) {
   74:     $id = textId2Id($tour);
   75:     
   76:     $sth = mysql_query("SELECT * FROM Tournaments WHERE Id=$id") or die(mysql_error());
   77:     $tournament = mysql_fetch_assoc($sth);
   78:     return $tournament;
   79: }
   80: 
   81: function textId2Id($tour) {
   82:     if (preg_match('/\./',$tour))
   83:     {
   84: 	$temp = explode('.', $tour);
   85: 	$fname=$temp[0];$n=$temp[1];
   86: 	$sth = mysql_query(
   87: 		$sql="SELECT t2.Id FROM Tournaments as t1, 
   88:                         Tournaments as t2 
   89: 			WHERE (t1.FileName = '$fname.txt' OR t1.FileName='$fname')
   90:                         AND t1.Id=t2.ParentId AND t2.Number=$n") or die(mysql_error().": $sql");
   91:     }	
   92:     else 
   93:     {
   94: 	$sth=mysql_query($sql="SELECT Id FROM Tournaments WHERE FileName = '$tour.txt' OR  FileName = '$tour'") or die (mysql_error().": $sql");
   95:     }
   96: 
   97:     $res = mysql_fetch_row ($sth);
   98:     $id=$res[0];
   99:     return $id;
  100: }
  101: 
  102: ?>

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