PDA

View Full Version : Modify Text files



locbtran
10-04-2012, 08:34 AM
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");
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

bernie1227
10-04-2012, 09:05 AM
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:


$file = fopen('test.txt', 'r');
for($i=0;$i<=count(items);$i++){
fwrite($file, items[$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:


$sql = mysql_query("SELECT * FROM yourtable");
$items = mysql_fetch_array($sql);

traq
10-04-2012, 03:04 PM
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.

locbtran
10-04-2012, 06:08 PM
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.

traq
10-04-2012, 08:10 PM
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.

bernie1227
10-04-2012, 10:08 PM
this (http://dev.mysql.com/doc/refman/5.1/en/reloading-delimited-text-dumps.html) may help you in createing a delimeter between the lines in the txt file.