Location: PHPKode > projects > Confrontation Tournament Database > process.php
<html>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head><title>Edit database</title></head>
<body background="background.jpg">
<center>
<img src="ix_newlogo.png"><br>
<h1>Modify database</h1></center>


<?php
		
	require_once('../minixml-1.3.0/minixml.inc.php'); // replace with your miniXML directory
	
	
	$xmlDoc =& new MiniXMLDoc();
	$passwordDoc =& new MiniXMLDoc();
 
	$xmlDoc->fromFile('database.xml'); // put your database XML file's name here
	$passwordDoc->fromFile('passwords.xml'); // put your passwords containing XML file's name here
	$xmlRoot =& $xmlDoc->getRoot();
	$passwordRoot =& $passwordDoc->getRoot();
	
	$authentication = FALSE;
	
	$users =& $passwordRoot->getElement('users');
	$user =& $users->getAllChildren();
	
	$postUser = $_POST['db_user'];
	$postPass = $_POST['db_pass'];
	
		
	
	for ($x=0;$x<$users->numChildren();$x++)
	{
		$chkUser=$user[$x]->getElement('name');
		$chkPass=$user[$x]->getElement('password');
		if ($chkUser->getValue() == $postUser)
		{
			if ($chkPass->getValue() == $postPass)
			{
				$authentication = TRUE;
				print '<b>Logged in: '.$postUser.'</b> <br><a href="process.php">Logout</a><br>';
			}
		}
	}
	
	if ($authentication == TRUE)
	{
	print '<br>
This script is far from perfect at the moment. Only rudimentary security has been added, and <b>all entries must be
entered with exact correctness</b> in order to avoid messing up the database. <b>No umlauts are allowed at this moment!</b><i> (ts. ei ääkkösiä)</i>.<br><br>
Script (c) 2006 by Joonas Joensuu (aka. Silveri).<br><br>
	<center>';
	
	
	$databaseChanges = FALSE;

	$players =& $xmlRoot->getElement('players'); // Get the "players" element from the database
	
	$awards =& $xmlRoot->getElement('awards');
	$award =& $awards->getAllChildren();
	$awardAmount =& $awards->numChildren();
		
		
	$player = $players->getAllChildren(); // Separate individual player element to the array $player[x]

	$playerAmount = $players->numChildren(); // Get a variable for defining a table for visual results

	$tournaments =& $xmlRoot->getElement('tournaments');
	
	$tournament = $tournaments->getAllChildren();
	$tournamentNumber = $tournaments->numChildren();
	
	
	// If no information is submitted, start by selecting between creating players or tounraments
	if ($_POST['entry_type'] == null)
	{
		print '<form name="select_type" action="process.php" method="post"><fieldset><legend>Create new</legend>
			<input name="db_user" type="hidden" value="'.$postUser.'">
		<input name="db_pass" type="hidden" value="'.$postPass.'">
			<input type="radio" name="entry_type" value="select_player">Create a new player<br>
			<input type="radio" name="entry_type" value="select_tournament">Create a new tournament<br>
			<button type="submit">Submit</button></fieldset></form>';
	}
	
	// If player creation selected
	
	if ($_POST['entry_type'] == "select_player")
	{
		print '<form action="process.php" method="post" name="player_info" target="_blank">
		<fieldset><legend>New Player</legend>
		<input name="db_user" type="hidden" value="'.$postUser.'">
		<input name="db_pass" type="hidden" value="'.$postPass.'">
		<input name="entry_type" type="hidden" value="create_player">
		New Player Name: <input name="player_name" type="text">
		<button type="submit">Add</button></fieldset>
		</form>';
	}
	
	if ($_POST['entry_type'] == "create_player")
	{
		if ($_POST['player_name'] !== null)
		{
		$newPlayer =& $players->createChild('player');
		$newPlayerName =& $newPlayer->createChild('name');
		$submittedPlayerName = $_POST['player_name'];
		$newPlayerName->text($submittedPlayerName);
		$newTourneys =& $newPlayer->createChild('tourneys');
		$databaseChanges = TRUE; // determine that changes have been made that need saving	
		}
	}
	
	
	// If tournament creation selected
	
	if ($_POST['entry_type'] == "select_tournament")
	{
		// Define Form
	
		print '<form name="player_input" action="process.php" method="post">';
	
		print '<input name="entry_type" type="hidden" value="entry_tournament">';
	
		print '<fieldset><legend>Basic tournament information</legend>
		<input name="db_user" type="hidden" value="'.$postUser.'">
		<input name="db_pass" type="hidden" value="'.$postPass.'">
	
			Name:<input name="tournament_name" type="text"><br>
			Date:<input name="tournament_date" type="text"><br>
			Rounds:<input name="tournament_rounds" type="text"><br>
			Maximum available GA:<input name="tournament_maxga" type="text"><br>
			Number of participants:<input name="tournament_playeramount" type="text"><br>
			Number of special awards given:<input name="tournament_awardamount" type="text"><br>
			Open:<input type="radio" name="tournament_open" value="no">No <input type="radio" name="tournament_open" value="yes">Yes<br>
			
			
			<button type="submit">Submit</button></fieldset></form>';
	
	}
	
	// If tournament information submitted, show submitted data and create fields for players
	
	if ($_POST['entry_type'] == "entry_tournament")
	{
		
		// TODO:	Check main database whether number of players exceeds the actual players
		//			in the database
		
		
		$processPlayers = $_POST['tournament_playeramount'];
	
		// Define Form
	
		print '<form name="player_input" action="process.php" method="post"><input name="db_user" type="hidden" value="'.$postUser.'">
		<input name="db_pass" type="hidden" value="'.$postPass.'">';
	
		print '<input name="entry_type" type="hidden" value="entry_players">';
	
		// Show results of previous input
		
		print '<fieldset><legend>Basic tournament information</legend>
	
			Name:<input name="tournament_name" type="hidden" value="'.$_POST['tournament_name'].'">'.$_POST['tournament_name'].'<br>
			Date:<input name="tournament_date" type="hidden" value="'.$_POST['tournament_date'].'">'.$_POST['tournament_date'].'<br>
			Rounds:<input name="tournament_rounds" type="hidden" value="'.$_POST['tournament_rounds'].'">'.$_POST['tournament_rounds'].'<br>
			Maximum available GA:<input name="tournament_maxga" type="hidden" value="'.$_POST['tournament_maxga'].'">'.$_POST['tournament_maxga'].'<br>
			Number of participants:<input name="tournament_playeramount" type="hidden" value="'.$_POST['tournament_playeramount'].'">'.$_POST['tournament_playeramount'].'<br>
			Number of special awards given:<input name="tournament_awardamount" type="hidden" value="'.$_POST['tournament_awardamount'].'">'.$_POST['tournament_awardamount'].'<br>
			<input name="tournament_open" type="hidden" value="'.$_POST['tournament_open'].'">
			Is tournament an open: '.$_POST['tournament_open'].'</fieldset>';
		print '<fieldset><legend>Input players</legend>';
	
	
		for ($q=0; $q < $processPlayers; $q++)
		{
			$InputName = "player".($q+1);
			$InputVP = "vp".($q+1);
			$InputGA = "ga".($q+1);
			$InputArmy = "army".($q+1);
			$InputAbort ="abort".($q+1);
			
			print ($q+1).'. <select name="'.$InputName.'">';
		
			for ($p=0; $p < $playerAmount; $p++)
			{
				$optionName = $player[$p]->getElement('name');
				print '<option value="'.$optionName->getValue().'">'.$optionName->getValue().'</option>';
			}
		
			print '</select>';
			print ' VP <input name="'.$InputVP.'" type="text"> ';
			print 'GA <input name="'.$InputGA.'" type="text">';
			print ' Army <input name="'.$InputArmy.'" type="text">';
			print '<input type="checkbox" name="'.$InputAbort.'">Aborted';
			print '<br>';
		}
		
	
		print"</fieldset>";
		
		// Award selection
		
		print "<fieldset><legend>Awards</legend>";
		$processAwards = $_POST['tournament_awardamount'];
		
			
		
		for ($q=0; $q < $processAwards; $q++)
		{
			$InputName = "award_receiver".($q+1);
			$InputAward = "award_type".($q+1);
			
			// List Awards
			
			print 'Type <select name="'.$InputAward.'">';
		
			for ($p=0; $p < $awardAmount; $p++)
			{
				$optionName = $award[$p]->getElement('long');
				$optionDesignation = $award[$p]->getElement('short');
				
				print '<option value="'.$optionDesignation->getValue().'">'.$optionName->getValue().'</option>';
			}
		
			print '</select> Receiver <select name="'.$InputName.'">';
			
			for ($p=0; $p < $playerAmount; $p++)
			{
				$optionName = $player[$p]->getElement('name');
				print '<option value="'.$optionName->getValue().'">'.$optionName->getValue().'</option>';
			}
			print '</select><br>';
			
		}
		
	
		print"</fieldset>";
		
		print '<button type="submit">Send</button>';
		print "</form>";
				
	}
	
	
	// Now that the player entries have been made, it's time to shove them into the database
	// to create a new tounrament
	
	
	if ($_POST['entry_type'] == "entry_players")
	{
		// Determine next Id
		
		if ($tournamentNumber > 0)
		{
			$latestIdValue = $tournament[($tournamentNumber-1)]->getElement('id');
			$newId = ($latestIdValue->getValue())+1;
		}
		else
		{
			$newId = 0;
		}
		
		// Firstly, create an entry for the tournament itself
		
		$newTournament =& $tournaments->createChild('tournament');
		$newTournamentName =& $newTournament->createChild('name');
		$newTournamentId =& $newTournament->createChild('id');
		$newTournamentDate =& $newTournament->createChild('date');
		$newTournamentRounds =& $newTournament->createChild('rounds');
		$newTournamentMaxga =& $newTournament->createChild('maxga');
		$newTournamentOpen =& $newTournament->createChild('open');
		$newTournamentAwards =& $newTournament->createChild('awards');
	
		// Add basic data
	
		$newTournamentName->text($_POST['tournament_name']);
		$newTournamentId->text($newId);
		$newTournamentDate->text($_POST['tournament_date']);
		$newTournamentRounds->text($_POST['tournament_rounds']);
		$newTournamentMaxga->text($_POST['tournament_maxga']);
		$newTournamentOpen->text($_POST['tournament_open']);
	
		// Then create a script for awards
		
		for ($a=0; $a < ($_POST['tournament_awardamount']); $a++)
		{
			
			
			$typeVar = "award_type".($a+1);
			$recVar = "award_receiver".($a+1);
			
			$newAward =& $newTournamentAwards->createChild($_POST[$typeVar]);
			$newAward->text($_POST[$recVar]);
			
		}
		
		// Second, create entry for each participant
		
		for ($p=0; $p < $_POST['tournament_playeramount'];$p++)
		{
			$postPlayer = "player".($p+1);
			$varPostPlayer = $_POST[$postPlayer];
			
			for ($a=0; $a < $playerAmount; $a++)
			{
				$dbPlayer = $player[$a]->getElement('name');
				$dbName = $dbPlayer->getValue();
				if ($varPostPlayer == $dbName)
				{
					$pointerTourneys =& $player[$a]->getElement('tourneys');
					
					// Create Elements
					
					$newTourney =& $pointerTourneys->createChild('tourney');					
					$newTourneyId =& $newTourney->createChild('id');
					$newTourneyArmy =& $newTourney->createChild('army');
					$newTourneyScores =& $newTourney->createChild('scores');
					$newTourneyRank =& $newTourneyScores->createChild('rank');
					$newTourneyVP =& $newTourneyScores->createChild('VP');
					$newTourneyGA =& $newTourneyScores->createChild('GA');
					
					// Shove the data in
					
					$pointerVP = "vp".($p+1);
					$pointerGA = "ga".($p+1);
					$pointerArmy = "army".($p+1);
					$pointerAbort ="abort".($p+1);
					
					//print $pointerArmy." ".($_POST[$pointerArmy])."<br>";
					
					if ($_POST[$pointerAbort] != "on")
					{
						$newTourneyRank->text($p+1);
					}
					else
					{
						$f = "999";
						//print $f;
						$newTourneyRank->text($f);
					}
					
					$newTourneyId->text($newId);
					$newTourneyVP->text($_POST[$pointerVP]);
					$newTourneyGA->text($_POST[$pointerGA]);
					$newTourneyArmy->text($_POST[$pointerArmy]);
					
					//print $pointerVP."/".$pointerGA."<br>";
					
				}
			}
		
		}
		
		
		// Lastly, show that database has been changed
		
		$databaseChanges = TRUE;
	
	}
	
	
			
	
	
	
	
	
	
	
	
		
	// Save Results
	
	if ($databaseChanges == TRUE)
	{
		$copyPaste = $xmlDoc->toString();
		
		//print $copyPaste;	
		
		$outputFile = fopen ('turnausdata.xml',"w");
		fwrite ($outputFile, $copyPaste);
		fclose ($outputFile);
	
		$poohpooh = new MiniXMLDoc();
		$poohpooh->fromFile('turnausdata.xml');
		print '
		<form name="hidden" action="process.php" method="post"><input name="db_user" type="hidden" value="'.$postUser.'">
		<input name="db_pass" type="hidden" value="'.$postPass.'"><button type="submit">New entry</button></form>
		Changes saved<br><a href="index.php">Return to index</a><br>';
	}
	}
	else
	{
		print '<form name="login" method="post" action="process.php">
				<fieldset>
				Name:<input type="text" name="db_user"><br>
				Password:<input type="password" name="db_pass"><br>
				<button type="submit">Login</button></fieldset></form><br><a href="index.php">Return to index</a>';
	}
	
	?>
</center>
<br>

</body>
</html>
Return current item: Confrontation Tournament Database