Page 1 of 1

cs out put problem

Posted: Mon Nov 07, 2005 10:11 am
by hame22
Hi i have a problem with my csv export script.

What is meant to happen is when i press a button on my web page a csv file containing transaction data is downloaded. This all works fine but at the bottom of the csv file there is the code from the web page.

Does anyone know why this has happened??

my script is as follows;

Code: Select all

function csv()
{
	db_connect();
	
	$result = mysql_query("select * from transactions");
while($row = mysql_fetch_array($result))
{
		$trans_id = $row['trans_id'];
		$user_id = $row['user_id'];
		$product_id = $row['product_id'];
		$trans_date = $row['trans_date'];
		$free_download = $row['free_download'];
		
		$row2 = activity_query($product_id);
		$title = $row2['title'];
		$price = $row2['price'];
	
$data .= "$trans_id, $user_id, $product_id, $trans_date, $free_download\n";
}
$filename = "somefile.csv";
header ('Content-type: text/csv');
header ('Content-Disposition: attachment; filename='.$filename);
echo $data; 
}

Posted: Mon Nov 07, 2005 7:24 pm
by feyd
the script continues to process after your function ends.. so if you have HTML output, it will get sent into the stream... use exit() to terminate the script after you have finished the file's output.