Results 1 to 6 of 6

Thread: Modify Text files

  1. #1
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Modify Text files

    I figure out how to write all the entries from my database onto a text file.

    Code:
    header("Content-Type: text/plain");
    header("Content-Disposition: Attachment; filename=test.txt");
    header("Pragma: no-cache");
    exit;
    How do I modify the text file??
    Each entries (or row) have 5 elements in it and there are multiple entries in my database.
    How do I write each elements follow by a comma onto a text file? Each row in the database would have a new line in my text file as well.

    tks

  2. #2
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    A better way to do it would be to put the contents of the database in an array, and then write it to the file using a for loop, so imagine you have an array called items:
    PHP Code:
    $file fopen('test.txt''r');
    for(
    $i=0;$i<=count(items);$i++){
        
    fwrite($fileitems[$i] + ',');

    First of all of course, you have to have your database items in an array, so to do that, you'd have something like:
    PHP Code:
    $sql mysql_query("SELECT * FROM yourtable");
    $items mysql_fetch_array($sql); 
    Last edited by bernie1227; 10-04-2012 at 09:18 AM.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Of course, if your database is very large, this could become very slow very quickly.

    What are you actually trying to accomplish? Perhaps we'll be able to offer better advice.

  4. #4
    Join Date
    Mar 2010
    Location
    Canada
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by traq View Post
    Of course, if your database is very large, this could become very slow very quickly.

    What are you actually trying to accomplish? Perhaps we'll be able to offer better advice.

    This is a very big database which I'm writing for a client with over a million entries so writing to an array might not be the best option.
    They would like to be able to write part of their data onto a text file. So a text file that is generated can have 100 entries or over 100,000 entries in it.

    So if I use fwrite, to write to my text file
    How would I be able to break up each entries, so I can separate them with a comma for readability
    So a row can have let say 3 entries and each entries can have more then one word in it

    Example:
    Name | Age | Country
    John Adam Smith | 37 | USA
    Steve Jones | 22 | Canada
    Mary Gray Thomas Faucer | 45 | USA

    Also, how do I know when I reach the end of the row, so I can add in a new line.

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by locbtran View Post
    They would like to be able to write part of their data onto a text file.
    For what purpose?

    database backups?
    creating a report (e.g., metrics)?
    caching search results, or creating an index?
    downloading|transferring records to another program?
    some internal (on the webserver) use?

    Trying to answer your question without knowing what your goal is would not be very helpful. It's likely that whatever we suggest wouldn't produce the results you needed - and therefore, it would be a waste of time.

    Even your descriptions so far have been vague and inconsistent - you talk about a comma-separated format, then give an example using pipes ( | ). The former suggests that you want to transfer the data to another database/program; while the latter suggests you're trying to format it for viewing as plain text.

    Please give as as much information as possible, so we can offer useful advice.

  6. #6
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    this may help you in createing a delimeter between the lines in the txt file.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

Similar Threads

  1. mail both files and text
    By weiner769 in forum PHP
    Replies: 0
    Last Post: 02-11-2010, 11:37 PM
  2. Searching text files
    By rachelk in forum PHP
    Replies: 2
    Last Post: 08-02-2009, 07:11 AM
  3. Replies: 19
    Last Post: 04-20-2008, 04:28 AM
  4. PHP Text Files...
    By VictorProfundus in forum PHP
    Replies: 4
    Last Post: 12-15-2007, 02:35 AM

Bookmarks

Posting Permissions

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