Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Mar 2005
    Location
    UK
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Write to text file/spreadsheet

    Hi,

    Im trying to make a PHP script that writes a few fields, say name and email, to either a text, spreadsheet or simelar file.

    Can anyone help me with this?

    Thanks in advance,
    Last edited by ItsMe; 06-20-2005 at 12:04 PM.
    Andrew,
    AKA ItsMe

  • #2
    New Coder
    Join Date
    Mar 2005
    Location
    UK
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I know this is possible to write to a single file once, but i want to write to a file many times, so each time it is submitted it adds the information on the end, not overwrites whats already there.

    Is this possible?
    Andrew,
    AKA ItsMe

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can append information to the end of existing information in a file by using the "a" argument in your fopen statement, eg fopen($file, "a"). Here's a little script that should give you the idea:

    PHP Code:
    $file 'your_filename.txt';
    $content "Some content";

    if (!
    $handle fopen($file'a')) {
        exit(
    "Could not open file: $file");
    }

    if (
    fwrite($handle$content) === FALSE) {
        exit(
    "Cannot write to file: $file");
    }

    fclose($handle); 

  • #4
    New Coder
    Join Date
    Mar 2005
    Location
    UK
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, i will look at that.
    Andrew,
    AKA ItsMe

  • #5
    Regular Coder
    Join Date
    Nov 2004
    Location
    The Netherlands
    Posts
    551
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if you want to make csv files, you can use the implode function to put strings together on one line seperated by anything you want.

    http://www.php.net/implode
    CATdude about IE6: "All your box-model are belong to us"

  • #6
    New Coder
    Join Date
    Mar 2005
    Location
    UK
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the help, but im still a bit unsure. I will explain exactly what i want it to do.

    There is a form in which users can enter their detail (name, email, website etc.) and this writes it to a file on the server.

    There is then a page which reads the data file and displays each persons details in a table.

    Can anyone give me some help on achieving this?
    Andrew,
    AKA ItsMe

  • #7
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To save the info, try:
    PHP Code:
    <?

    // Define file to save to:
    $fileLocation '/path/to/file.txt';

    // Implode the $_POST array,
    // Save each value in format "Value0 || Value1 || Value2" etc:
    $savePost implode(' || '$_POST);

    // Open file (defined above),
    // Set action to 'a' for 'append' - won't overwrite existing file contents: 
    $fp fopen($fileLocation,'a') or die('Could not open file.');

    // Lock file temporarily so nobody can access it while it's being written to:
    flock($fp,LOCK_EX);

    // Write $savePost to a new line in the file:
    fwrite($fp,$savePost."\n");

    // Unlock the file so it can be used again:
    flock($fp,LOCK_UN);

    // Close file:
    fclose($fp);
        
    ?>
    Then to read it in the future, try:
    PHP Code:
    <?

    // Define file to read:
    $fileLocation '/path/to/file.txt';

    // Read the file into an array by line:
    $fileContents file($fileLocation);

    //Start table:
    echo '<table><tr>';
    echo 
    '<td>NAME</td>';
    echo 
    '<td>EMAIL</td>';
    echo 
    '<td>PHONE</td>';
    echo 
    '</tr>';

    // Loop thru the file by line:
    foreach($fileContents as $line) {
         
    // Trim newline \n and other whitespace:
         
    $line rtrim($line);
         
    // Explode Values into an array:
         
    $values explode(' || '$line);
         
    // Print the results:
         
    echo '<tr>';
         for(
    $n=0$n<count($values); $n++) echo '<td>'.$values[$n].'</td>';
         echo 
    '</tr>';
    }

    // End table:
    echo '</table>';

    ?>
    I haven't tested this so I may have made a silly mistake/typo, but the general idea is there.

    To recap, let's say a form has values 'Name', 'Email', 'Phone'

    The first part reads the Posted form, saves the values to $savePost like this: Name || Email || Phone
    Then it writes that to a new line of the file you're using to save all this info

    The second part reads the file by line, and for each line
    explodes the Name || Email || Phone string into an array.
    Then it prints the values to a table:

    Name 1 Email 1 Phone 1
    Name 2 Email 2 Phone 2
    Name 3 Email 3 Phone 3

    Hope this helps!
    MTD
    Last edited by mtd; 09-04-2005 at 03:03 PM. Reason: Fixed some code errors mentioned in this discussion

  • #8
    New Coder
    Join Date
    Mar 2005
    Location
    UK
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That code looks great, i will look into it further and test it later.

    So all i do is add a form page that submits the variables Name, Email and Phone to the first PHP script you have shown me?

    Thanks very much
    Andrew,
    AKA ItsMe

  • #9
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If all goes as planned, that is correct. submit the form to whatever php page has the first bit of code. FYI, you may need to alter the permissions (777) of the file you are writing to, but try it normal first because it often depends on server permissions.

    Another thing I thought of. $_POST will likely include the value for the Submit button (i.e. $button = true). If you don't want that to be printed to the table, try adding this to the first part of the code between $fileLocation and $savePost:
    PHP Code:
     // Remove last form element (submit button): 
         
    $lastElement array_pop($_POST); 
    The array_pop removes the last element from the $_POST array, so assuming the Submit button is the last form element (usually the case), it will be removed from the $_POST and will not be saved in $savePost. Depending on how you are implementing the table in context of the big picture, this may be important or not so in the least... your call.

    Hope it works for you. If you need any more help, let me know. (I'm not exactly a pro, so the practice helps)

  • #10
    New Coder
    Join Date
    Mar 2005
    Location
    UK
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks very much, looks great. will implement it later and let you know how it goes.
    Andrew,
    AKA ItsMe

  • #11
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also know that you can use any inputs that you want. They don't have to be limited to or include NAME, EMAIL, PHONE. The only part that is hard-coded is at the beginning of the second script, where it sets up the table headers (echo '<td>NAME</td>'). You would just have to edit those headers for whatever inputs you have, in the order that they are posted from the form. Or, you could just do away with those headers, but I personally find it easier to always label the info in my data tables.

  • #12
    New Coder
    Join Date
    Mar 2005
    Location
    UK
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi,

    I have just tested this script and all seems to be great apart from i get a parse error on the following line of code on the write script:

    PHP Code:
    // Unlock the file so it can be used again: 
    flock($fp,LOCK_UN); 
    I cannot see anything wrong with this code, can you?

    Thanks again
    Andrew,
    AKA ItsMe

  • #13
    New Coder
    Join Date
    Mar 2005
    Location
    UK
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi again,

    I have solved the parse error problem, i found a ; missing on the line above. now i am getting the following 2 errors after i submit my form to the write page.

    Errors:
    Warning: Bad arguments to implode() in write.php on line 16

    Warning: Wrong parameter count for fwrite() in write.php on line 26

    Any ideas?

    Thanks
    Andrew,
    AKA ItsMe

  • #14
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I edited my original code to include the following updates:

    - Added missing ; that you mentioned
    - fixed fwrite() error - the problem was that I forgot to reference $fp. It used to read fwrite($savePost."\n"); and now (correctly) reads fwrite($fp,$savePost."\n");

    I still have to look into the Bad arguments error, I'll let you know if I find anything. Like I said, I didn't test the code (I actually wrote it write here on the forum), so I do appologize for those errors.

  • #15
    mtd
    mtd is offline
    Regular Coder
    Join Date
    Jun 2003
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tested the code in DW and on my server, and I don't get that implode error. I did, however, re-write the code in DW just to be safe...maybe I missed something before? Anyway, I attached the two pages that I used that seemed to work, see if you have the same trouble with those.
    Attached Files Attached Files


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •