Annotated code for the soccer.php script

See the commentary in the control file.

<html><head><title>Soccer!</title></head><body>
<?

function tableheader($caption) {
	print <<<TABLE
<table align=center border=2>
<caption>$caption</caption>
<tr>
	<th>Team1</th><th>Score</th><th>Team2</th><th>Score</th>
</tr>
TABLE;
}

Connect to database

The code is database specific, though changes are minor. This code uses an ODBC connection to a Microsoft Access database. Access does not demand names and passwords; other ODBC accessible databases do so the odbc_connect request requires such data.

function connect() {
	$user = "";
	$password = "";

	$db = odbc_connect( 'PHPEG' , $user, $password ); 
	
	if (!$db) die( "Error in odbc_connect"  ); 
        
	return $db;   
}

Report generation

function report($caption, $comment, $result) {
	$count = 0;
	// Can ask for number of rows returned, but
	// most drivers cannot supply this information

	while( odbc_fetch_row( $result) ) { 
		if($count==0) tableheader($caption); $count++;
  		$Team1 = odbc_result($result, "Name1");
 		$Score1 = odbc_result($result, "Score1");
  		$Team2 = odbc_result($result, "Name2");
 		$Score2 = odbc_result($result, "Score2"); 	


		print "<tr><td>$Team1</td><td>$Score1</td><td>$Team2</td><td>$Score2</td></tr>";
	}
	if($count>0) print "</table>";
	else print "<h3 align=center>$comment</h3>";
}

Processing a search request

function runtask($strings) {
	list($sqlstring, $caption, $comment) = $strings;
	$db = connect();
	$result = odbc_exec( $db,  $sqlstring);

  	if (!$result) 
		die( "Error in odbc_exec( no result returned ) " ); 
  
	report($caption, $comment, $result);
	print "</body> </html>";
	odbc_close( $db); 
}

Search requests

Search requests just require different SQL queries to be run by same function.

function homewins() {
	$home = array ( 
			"select * from Teams where Score1 > Score2",
			"Home wins",
			"No home wins yet this season"
		);
	runtask($home);
}

function awaywins() {
	$away = array ( 
			"select * from Teams where Score1 < Score2",
			"Away wins",
			"No away wins yet this season"
		);
	runtask($away);
}

function drawngames() {
	$drawn = array ( 
			"select * from Teams where Score1 = Score2",
			"Drawn games",
			"No drawn games yet this season"
		);
	runtask($drawn);
}

function listall() {
	$drawn = array ( 
			"select * from Teams",
			"Season's matches",
			"Season has not started yet"
		);
	runtask($drawn);
}

Generate form for adding data

function doadd() {
	print <<<ADDER
<form action="Soccer.php" method=post>
<table align=center border=2>
<caption>Enter result of match</caption>
<tr>
	<th>Home team</th><th>Score</th><th>Away team</th><th>Score</th>
</tr>
<tr>
	<td><input type=text size=20 name=Team1></td>
	<td><input type=text size=3 name=Score1></td>
	<td><input type=text size=20 name=Team2></td>
	<td><input type=text size=3 name=Score2></td>
</tr>
<tr>
	<td colspan=4 align=center><input type=submit name=add value="Add data">
</tr>
</table>
</body>
</html>
ADDER;
}

Addition of data

function checkadd() {
	global $HTTP_POST_VARS;
	$msg = "Invalid or incomplete data; cannot add result";
	$Team1 = $HTTP_POST_VARS["Team1"];
	$Team2 = $HTTP_POST_VARS["Team2"];
	$Score1 = $HTTP_POST_VARS["Score1"];
	$Score2 = $HTTP_POST_VARS["Score2"];

	if(!(isset($Team1) && isset($Team2) && isset($Score1) && isset($Score2)))
		die($msg);

	$Score1 = (integer) $Score1;
	$Score2 = (integer) $Score2;

	$db = connect();


	$sql_string = "insert into Teams Values( '$Team1', '$Team2', $Score1, $Score2 )";
	
	$result = odbc_exec($db, $sql_string);
	if(!$result) die("Failed to update database");
	print <<<ADDED
<p>Result added to database.
</body>
</html>
ADDED;
	odbc_close($db);


}

Generate form in response to GET request

function mainform() {
	print <<<FORMSTUFF
<h1>Soccer league keeper</h1>
You may review the existing contents of the database or
add data.
<br>
<form action="Soccer.php" method=post>
<input type=radio name=choice value=add>Add data
<br>
<input type=radio name=choice value=list checked>List all data
<br>
<input type=radio name=choice value=draw>List drawn games
<br>
<input type=radio name=choice value=home>List home wins
<br>
<input type=radio name=choice value=away>List away wins
<br>
<input type=submit value="DO IT">
</form>
</body>
</html>
FORMSTUFF;
}

Main function for script

Determine whether a "get" (form request) or "post" (form processing) request.

if(!empty($HTTP_POST_VARS)) {
	$choice = $HTTP_POST_VARS["choice"];
	if($choice=="home") { homewins(); exit(); }
	elseif($choice=="away") { awaywins(); exit(); }
	elseif($choice=="draw") { drawngames(); exit(); }
	elseif($choice=="list") { listall(); exit(); }
	elseif($choice=="add") { doadd(); exit(); }
	elseif(!isset($choice)) { checkadd(); exit(); }
}
else {
	mainform();
	exit();
}

?>