Jump to content

Export Data to Textfile


lilywong

Recommended Posts

i found a solution of this, but the data output is all in one line, i have  use the "\n", but everything still printed out in one line, just wondering, i wanna the record to be printed out in many lines based on different ID.


for ($i = 0; $i < $fields; $i++) {
    $header .= mysql_field_name($export, $i) . "|";
}

while($row = mysql_fetch_row($export)) {
    $line = '';
    foreach($row as $value) {
        if ((!isset($value)) OR ($value == "")) {
            $value = "|";
        } else {
            //$value = str_replace('|', '', $value);
            $value = "|".$value."|";
        }
        $line .= $value;
    }
    //$data .= trim($line). "\n";
    $data .= $line."\n";
}
//$data = str_replace("\r","",$data);

if ($data == "") {
    $data = "\n(0) Records Found!\n";
}

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.txt");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
if you just want a simple text file, you could just loop through the records you want and build a string formatted the way you want and then the code below is an example of how to create the actual text file

[code]
$fh = fopen('FILE_PATH', 'w') or die("ERROR: can't open file for writing");
fwrite($fh, $NEW_FORMATTED_STRING);
fclose($fh);
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.