1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Rep Power

    Modify Text files

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

    header("Content-Type: text/plain");
    header("Content-Disposition: Attachment; filename=test.txt");
    header("Pragma: no-cache");
    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.

  2. #2
  3. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    West Yorkshire
    Rep Power
    I am guessing that you want your text file to end up something like this example :-

    Firstly, grab the data from your database and stuff it into an array.
    Then using a while loop, go through all the elements (rows) in that array, writing to your text file as you go.

    You can "implode" an array by using the PHP command implode
    And then write the line(s) to your text file using PHP command file_put_contents

    So let's say our database is like this:-

    Table : "likes"
    col1 | col2 | col3 | col4 | col5
    richard | sheep | dog | cat | food
    mark | clouds | cups | monitor | radio
    john | fish | orange | bubbles | telephone

    Firstly, we get all the data in there into an array, this example will use the "mysql" library but I wouldn't recommend using it in production as it's now old and you should use mysqli or PDO ...

    PHP Code:
    $sql "SELECT * FROM `likes`;";
    $result mysql_query($sql); 
    The next thing we check to ensure there were some records returned and if not, stop the script (you can of course, use error handling but for this example we're going to kill it, dead ...

    PHP Code:
    if (!$result) { die("No results"); } 
    So next we know we have data in the $result variable and we need to shove that into an array ... at the same time, we're going to loop through the data and create a single line which we can use later to write into our .TXT file ...

    PHP Code:
    while ($row mysql_fetch_assoc($result)) {
    $data[] = implode($row ",");

    At this point, we have an array called $data which has (in our case) 3 "rows". Example:-

        [0] => richard,sheep,cats,dogs,food
        [1] => mark,clouds,cups,monitor,radio
        [2] => john,fish,orange,bubbles,telephone
    So now it's a case of writing these lines to a text file ...

    PHP Code:
    $file "likes.csv";
    foreach (
    $data as $key => $value) {
    $current file_get_contents($file);
    $current .= "{$value}\n";

    "likes.csv" now has a comma separated list of records from your database.
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984

IMN logo majestic logo threadwatch logo seochat tools logo