Jump to content

[SOLVED] CSV effiecient file creation


Asheeown

Recommended Posts

<?php

$fh = fopen('include/temp/temp.csv', "w") or die("PROBLEM!");

// Create a header row in the file
fwrite($fh, "Source,Called,Destination,Ani,Call Time\n") or die("WE GOT A PROBLEM WRITING!");

// Loop through the results, writing to the file
while ($row = mysql_fetch_assoc($UserRated1)){
$data = $row['Originating_TG'] . "," . $row['Inpulsed_Digits'] . "," . $row['IB_Region_Name'] . "," . $row['ANI'] . "," . $row['UTCTime'] . "\n";
fwrite($fh, $data);
}
// Close the file
fclose($fh);

// Compress a file
compress("include/temp/temp.csv", "include/temp/temp.csv.gz");
echo "<a href=\"include/temp/temp.csv.gz\">Download link </a><br />";
?>

 

 

All the links are just temporary till I get a finished product and I can get a temp file script.

What does the query look like that you're using to pull the data from the database? If you are just pulling the fields you are writing, you can using implode() to put the commas between the fields. Also storing all the data in temporary array and then doing one write might be faster:

<?php
$data = array();
while ($row = mysql_fetch_array($UserRated1)){
$data[] = implode(',',$row);
}
fwrite($fh, implode("\n",$data);
?>

 

I don't know if it's more efficient or not, but you're doing only one write instead of many.

 

Ken

 

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.