I'm relatively new to php, but I love it so far and want to get better at it. I'm in charge of a baseball tournament that my friends and I play on our xboxes and we save our stats into an excell sheet after every game. But, this year because one guy moved away we've had to write them out by hand and then I have to manually type each stat in which takes quite a bit of time.
I know php can do this a lot easier by writing the stats to a text file. Plus, I have it set up where it reads the roster from a txt file to save me a lot of long coding time, but I don't know how to write it to a text file where I'll have 13 categories separated by a tab and then start on another line.
I've been about to create the table ( you can check it out here).
My team txt file is set up where i have the batters on the left and the pitchers on the right separated by 2 tabs. There aren't as many pitchers as batters so that's why i have the blank table cells. (Know how to fix that???)
Basically I just want to write the php code so I can take the values that the user inputs into a text file. Here's the php code that makes the table. I thinks there's an easier way to do it, but I was following an example from a book i have.
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/2000/REC-xhtml-20000126/DTD/xhtml1-trasitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Stat Entry Form</title>
<style type="text/css">
<!--
td {
border: 1px dotted #CCCCCC;
font-family: tahoma;
font-size: 8pt;
color: #999999;
text-align: center;
}
-->
</style>
</head>
<body>
<?php
// Identify the file based on what team played
$file = $team . "_roster.txt";
// Read the file into and array
if ($players = @file ($file)) {
// If it read the file print the header row of table
echo '<form action="handle_stats.php" method="post">
<table width="80%" cellpadding="2" cellspacing="2">
<tr>
<td>NAME</td>
<td>AB</td>
<td>R</td>
<td>H</td>
<td>RBI</td>
<td>BB</td>
<td>SO</td>
<td>SF</td>
<td>2B</td>
<td>3B</td>
<td>HR</td>
<td>SB</td>
<td>CS</td>
</tr>
';
// Use an array to print out each player
foreach ($players as $key => $array) {
if ($key != 0) { // Don't print dummy line of txt file
// Turn each line into it's own array
$second_array = explode ("\t\t", $array);
// the first item is the batters
$batter = $second_array[0];
// print the table row for each player
echo " <tr>
<td>$batter</td>
<td><input name=\"ab[$batter]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"r[$batter]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"h[$batter]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"rbi[$batter]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"bb[$batter]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"so[$batter]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"sf[$batter]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"2b[$batter]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"3b[$batter]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"hr[$batter]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"sb[$batter]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"cs[$batter]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
</tr>\n";
}
}
// print header row for pitchers
echo "<tr>
<td>NAME</td>
<td>W</td>
<td>L</td>
<td>S</td>
<td>IP</td>
<td>H</td>
<td>R</td>
<td>ER</td>
<td>BB</td>
<td>SO</td>
<td>HR</td>
<td> </td>
<td> </td>
</tr>\n";
// use an array to print out each player
foreach ($players as $key => $array) {
// don't print the first line
if ($key != 0) {
// turn each line of the array into its own
$second_array = explode ("\t\t", $array);
// the second item is the pitchers but delete the return character
$pitcher = substr ($second_array[1], 0, (strlen($second_array[1]) - 1));
// print table row for each pitcher
echo " <tr>
<td>$pitcher</td>
<td><input name=\"w[$pitcher]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"l[$pitcher]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"s[$pitcher]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"ip[$pitcher]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"h[$pitcher]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"r[$pitcher]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"er[$pitcher]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"bb[$pitcher]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"so[$pitcher]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
<td><input name=\"hr[$pitcher]\" type=\"text\" value=\"0\" size=\"3\" maxlength=\"3\"></td>
</tr>\n";
}
}
// print submit button
echo "<tr><td colspan=\"13\" align=\"center\"><input type=\"submit\" name=\"Submit\" value=\"Submit\"></td></tr>
</table>
<input type=\"hidden\" name=\"team\" value=\"$team\">
</form>\n";
} else {
echo "Couldn't open the team file $file. Make sure the variable has a value.<br />";
}
?>
</body>
</html>
Last edited by jdubwelch; 04-01-2005 at 02:31 AM..
|