Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-04-2012, 09:21 AM   PM User | #1
locbtran
New Coder

 
Join Date: Mar 2010
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
locbtran is an unknown quantity at this point
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
__________________
HotDeals
locbtran is offline   Reply With Quote
Old 10-05-2012, 02:26 AM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,366
Thanks: 18
Thanked 348 Times in 347 Posts
sunfighter is on a distinguished road
I had an old file, dusted it off. It caused me a lot of nightmares so wrote from scratch.

PHP Code:
<?php
require('inc/DB_connect.php');   // Your connect to database
unlink('MyFile.cvs');            // used this because I was writing the file hunderds of times. You may not need
$fp fopen("MyFile.cvs""w");  // Opened the file for writting
$DbFile 'clan_members';        // Put the name of your table here

$result mysql_query("SELECT * FROM $DbFile");
$row mysql_fetch_assoc($result);

$line '';
$comma '';
foreach (
$row as $name => $value)
{
    
$line .= $comma '' str_replace('''""'$name);
    
$comma ',';
}
$line .= '\n';
fputs($fp$line);

mysql_data_seek($result0);
$line '';
while(
$row mysql_fetch_assoc($result))
{
    
$comma '';
    foreach (
$row as $name => $value)
    {
        
$line .= $comma str_replace('''""'$value);
        
$comma ',';
    }
    
$line .= '\n';
}
fputs($fp$line);
echo 
$line;
fclose($fp);
?>
sunfighter is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:04 AM.


Advertisement
Log in to turn off these ads.