Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save data to csv 1

Status
Not open for further replies.

bicyleman9008

IS-IT--Management
May 19, 2005
30
I am somewhat of a beginner with php code.

I have the enclosed php program, which dumps data to the screen. I would rather save it to a csv file on the local PC.


What do I need to change and add to redirect this?

******************
<?php
include "cfg/connect.inc.php";
include("./includes/database/".DBMS.".php");
db_connect(DB_HOST,DB_USER,DB_PASS) or die (db_error());
db_select_db(DB_NAME) or die (db_error());

$q = db_query( "select orderID, customerID, order_time, customer_ip, shipping_type, ".
" payment_type, customers_comment, statusID, shipping_cost, order_amount, ".
" order_discount, currency_code, currency_value, customer_email, ".
" shipping_firstname, shipping_lastname, ".
" shipping_country, shipping_state, shipping_zip, shipping_city, ".
" shipping_address, billing_firstname, billing_lastname, ".
" billing_country, billing_state, billing_zip, billing_city, ".
" billing_address, cc_number, cc_holdername, cc_expires, cc_cvv, shippingServiceInfo ".
" from SS_orders ORDER BY orderID DESC"
);


while( $row = db_fetch_row($q) ) {
$row=array_chunk($row, 33, TRUE);
$row[1]["cc_number"]= base64_decode ( $row[1]["cc_number"]);
$row[1]["cc_holdername"]= base64_decode ( $row[1]["cc_holdername"]);
$row[1]["cc_expires"]= base64_decode ( $row[1]["cc_expires"]);
$row[1]["cc_cvv"]= base64_decode ( $row[1]["cc_cvv"]);
while (list($key, $val) = each ($row[1])) {
echo "$key => $val<br>";
}
echo "<br>";

}

?>
 
something like this should work in place of your current while loop. it contains two different functions because fputcsv was only introduced in 5.1.0RC1. if that function does not exist the script automatically fails-safe to the fwrite variant.

Code:
<?
$filename = "filename.csv";		
if (file_exists($filename)):
	if(!is_writable($filename)):
		die ("The file: $filename is not writable");
	endif;
elseif( !is_writable( getcwd() ) ):
	die("you cannot create files in this directory.  Check the permissions");
endif;
	
//open the file for APPENDING
//add the "t" terminator for windows 
//if using a mac then set the ini directive
$fh = fopen($filename, "at");
//Lock the file for the write operation
flock($fh, LOCK_EX);
//loop through the recordset
while( $row = db_fetch_row($q) ) {
	//db output manipulation
	$row=array_chunk($row, 33, TRUE);
	$row[1]["cc_number"]= base64_decode ( $row[1]["cc_number"]);
	$row[1]["cc_holdername"]= base64_decode ( $row[1]["cc_holdername"]);
	$row[1]["cc_expires"]= base64_decode ( $row[1]["cc_expires"]);
	$row[1]["cc_cvv"]= base64_decode ( $row[1]["cc_cvv"]);

	//use fputcsv by preference - we can just pass the row-array
	if (function_exists("fputcsv")):
		fputcsv($fh, $row[1], ",", "\"");
	else: 
		//clean up the strings for escaping and add quotes
		foreach ($row[1] as $val):
			$b[] = '"'.addslashes($val).'"';
		endforeach;
		//comma separate the values
		$string = implode(",",$b);
		//write the data to the file 
		fwrite($fh, $string ."\n",strlen($string));
	endif;
}			
//close the file handle outside the loop
//this releases the lock too
fclose($fh); 
echo "Download the file <a href=\"$filename\">here</a>";

?>
 
In addition to jpadies excellent example:

If you want the "open or save" dialog to appear, then you can send these headers:

Code:
<?php
header('Content-type: application/excel');
header('Content-Disposition: attachment; filename="filename.csv"');
readfile('filename.csv');
?>

... at the end of the script (omit the echo line)

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top