Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 04-01-2005, 02:14 AM I need help writing to a txt file
Novice Talker

Posts: 10
Trades: 0
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>&nbsp;</td>
            <td>&nbsp;</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..
jdubwelch is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-01-2005, 03:24 AM
Skilled Talker

Posts: 62
Trades: 0
Does your host have access to a SQL such as MySQL by any chance?
It would be a lot easier, so I would check out that path before going and making a script to read the text file.
The Jasong is offline
Reply With Quote
View Public Profile
 
Old 04-01-2005, 03:29 AM
Novice Talker

Posts: 10
Trades: 0
Currently I don't... but I might in the next couple of weeks. I haven't used MySQL at all though. The 2 books I have cover it though.
jdubwelch is offline
Reply With Quote
View Public Profile
 
Old 04-01-2005, 03:49 AM
Skilled Talker

Posts: 62
Trades: 0
MySQL will be a lot easier and you will be able to get more help ( I may be able to help you with it a bit, but not with reading text files). So if you wait until you get mysql it will be worth it in the long run.
The Jasong is offline
Reply With Quote
View Public Profile
 
Old 04-01-2005, 05:04 AM
Novice Talker

Posts: 10
Trades: 0
I'm going to sign up tomorrow with a web host that has unlimited MySQL databases. So how'd I do it using MySQL. Would I read the rosters from the database too? or still use the text file?
jdubwelch is offline
Reply With Quote
View Public Profile
 
Old 04-01-2005, 05:56 PM I've got MySQL now, what do i do?
Novice Talker

Posts: 10
Trades: 0
How do i set up the database for what i'm trying to do?
jdubwelch is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to I need some help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24886 seconds with 12 queries