PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 4th, 2012, 03:28 AM
hiyatran hiyatran is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2010
Posts: 45 hiyatran User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 26 sec
Reputation Power: 3
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
__________________
OutingsForLess.com

Reply With Quote
  #2  
Old October 4th, 2012, 06:38 AM
badger_fruit's Avatar
badger_fruit badger_fruit is offline
Confused badger
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Location: West Yorkshire
Posts: 760 badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level)badger_fruit User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 5 h 15 m 18 sec
Reputation Power: 339
I am guessing that you want your text file to end up something like this example :-

Code:
richard,sheep,cats,dogs,food
mark,clouds,cups,monitor,radio
john,fish,orange,bubbles,telephone


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:-

Code:
Array
(
    [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";
    
file_put_contents($file$current);



"likes.csv" now has a comma separated list of records from your database.
__________________
The number for UK Emergencies is changing, the new number is 0118 999 881 999 119 7253

"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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Modify Text files

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap