cs out put problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

cs out put problem

Post 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; 
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply