PHP Forms - How do I format the output to a text file?

Discussion in 'PHP' started by LittleMike, Nov 4, 2011.

  1. #1
    Hello all,

    I have an HTML form with about 85 different fields in it (it's for a fantasy hockey league.) I've figured out how to save the output of the form to a text file via PHP using something like:

    <?php
    $saving = $_REQUEST['saving'];
    if ($saving == 1){
    $data = $_POST['Name'];
    $data .= $_POST['Team_Name'];
    $data .= $_POST['TeamN'];
    $data .= $_POST['League'];
    $data .= $_POST['WeekN'];
    etc... etc...
    $file = "hockey_data.txt";
    $fp = fopen($file, "a") or die("Couldn't open $file for writing!");
    fwrite($fp, $data) or die("Couldn't write values to file!");
    fclose($fp);
    echo "Request Successfully Sent."; #Confirmation form was submitted
    }
    ?>
    Code (markup):
    So that works exactly how I want. However, the text file obviously just has a dump of all the field data with no formatting. Is there a way to format that text so that it's more human readable? More specifically, can I arrange the data into columns? For example:

    Column 1---------------------------------Column 2
    Submitted on: November 5, 2011-----------Time: 4:00PM
    Name: Joe Blow---------------------------Team Name: The Whistlers
    Team #: 5--------------------------------Week: 1
    Roster:
    PLAYER1 PLAYER2 PLAYER3 PLAYER4 PLAYER5

    Etc, etc....

    Basically I want to format it so that a league manager can just open up the text file and see all the submissions.

    The only thing I could come up with would be to use standard HTML tags and php calls to the variables like:

    
    <h1 align=center>My Hockey Team</h1>
    Name: <?php echo $_POST["Name"]; ?></br>
    Team Name: <?php echo $_POST["Team"]; ?>
    Team #: <?php echo $_POST["TeamN"]; ?>
    Code (markup):
    etc... etc... and format it using CSS. That would only output the data to that same PHP page, though, wouldn't it? How do I still save it as a formatted text file? Is it possible to do both?

    The other issue I have is adding both a time and date submitted and then some sort of line break for the end of file so that when I append to it, it doesn't get lost in the previous submission data.

    I'm sorry, it sounds like a lot to ask to figure this out but I'm at my wit's end. I've seen plenty of posts here on how to save form data to a file, but nothing about actually formatting the text file.

    So in short, I would like to figure out how to:

    a)Show the user a confirmation message after clicking submit
    b)Save the form data to a text file
    c)Format the text file so it's more human-readable

    As-is, I have a and b down, but c is killing me!

    Thanks for any help anyone can offer.

    -Mike
     
    LittleMike, Nov 4, 2011 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    you can use \n for new lines, \t for tabs - they have to be within double quote strings " and not single quotes '

    That would work.

    Then if you did want to include in a page you can use nl2br() to put some html in.
     
    sarahk, Nov 5, 2011 IP
  3. LittleMike

    LittleMike Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Could you give me a quick example of that?
     
    LittleMike, Nov 5, 2011 IP
  4. RandyCandy

    RandyCandy Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I believe you can make it send it to your email
     
    RandyCandy, Nov 5, 2011 IP
  5. LittleMike

    LittleMike Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'm not looking to send to email. I'm looking to format the text file that it saves to.
     
    LittleMike, Nov 5, 2011 IP
  6. LittleMike

    LittleMike Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Still no answer on this one? Ugh, this thing is killing me.
     
    LittleMike, Nov 5, 2011 IP
  7. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #7
    <?php
    $saving = $_REQUEST['saving'];
    if ($saving == 1){
    $data = "name \t".$_POST['Name']."\n";
    $data .= "team name \t".$_POST['Team_Name']."\n";
    $data .= "team n \t".$_POST['TeamN']."\n";
    $data .= "League \t".$_POST['League']."\n";
    $data .= "Week n \t".$_POST['WeekN']."\n";
    etc... etc...
    $file = "hockey_data.txt";
    $fp = fopen($file, "a") or die("Couldn't open $file for writing!");
    fwrite($fp, $data) or die("Couldn't write values to file!");
    fclose($fp);
    echo "Request Successfully Sent."; #Confirmation form was submitted
    }
    ?>
     
    kmap, Nov 5, 2011 IP
  8. LittleMike

    LittleMike Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hey kmap, thanks. So that will put tabs and line breaks after each field into a text file. Are there any other formatting properties available or is that it? Like could I create a table with headers?
     
    LittleMike, Nov 5, 2011 IP
  9. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #9
    The problem with using a text file to create a table is that the length of each bit of data will be different and therefore that needs to be measured and calculated.

    Seriously... I'd store it in a database and when it's called generate the output file. Don't save it that way.
     
    sarahk, Nov 6, 2011 IP
  10. LittleMike

    LittleMike Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    The problem is this is for my friend's site and he's used to doing things a certain way. Originally I agreed to help because he had this 15 year old CGI script that did everything he wanted but then he moved webhosts and something broke. My perl skills are even less than my meager PHP skills, but I agreed to see if I could figure it out for him. The end results is that he "needs" the script to output a .dat file which really was just a text file that had everything formatted nicely. The script needs to take all 85 fields or so, output them to the file, which he then ftp's to his local machine to view and keep track of all the hockey line ups. The script also needs to be set up so that the text file is appended every time someone else makes a submission for a line up. My friend eventually cleans these out manually so that the file doesn't get too big.

    Using fopen/fwrite/fclose, I can output the fields to a file, but the formatting isn't very versatile. I'm really not sure I can convince him to allow me database access. Not unless it's completely hands-free on his part, and his workflow remains the same (ftp'ing over a text file to his desktop that is formatted properly.)

    Part of my just wants to tell him forget it, I can't do it :p

    EDIT - Also, as far as the data goes, the fields all have a maximum length in them for the most part. There is a comment box at the bottom that doesn't, but it's the end of the list, so formatting isn't as important for that field.
     
    Last edited: Nov 6, 2011
    LittleMike, Nov 6, 2011 IP