Jump to content

Export to program help


savagenoob

Recommended Posts

I wrote some code to export data from the website to a specific program, the extension of the files it uses is .dat. When I do the export on my local machine it works beautifully, but off of the hosted site it doesnt work. I am thinking its a header issue and maybe someone can help. Here is a chunk of code.

 

<?php
$date=date("m,d,Y");
$ID=$_GET['ID'];
$result = mysql_query("SELECT * FROM clients WHERE ID = $ID"); 
while($myrow = mysql_fetch_assoc($result)) 
             {
header("Content-Type: application/force-download");
header( "Content-Type:text/octet-stream" );
header('Content-disposition: attachment; filename="client.dat"');
?>
"0","0","0","0028836"
"1","37","1","<?php echo $myrow['First_Name'];?> <?php echo $myrow['Middle_Name'];?> <?php echo $myrow['Last_Name'];?>","<?php echo $myrow['Phys_Street'];?>","<?php echo $myrow['Phys_City'];?>","<?php echo $myrow['Phys_Zip'];?>","34","","35","<?php echo $myrow['Phone'];?>","1","2","CA - FSC RATER","<?php echo $date;?>","1641","32","<?php echo $myrow['Employer'];?>","<?php echo $myrow['Emp_Street'];?>  <?php echo $myrow['Emp_City'];?> <?php echo $myrow['Emp_State'];?> <?php echo $myrow['Emp_Zip'];?>","","","<?php echo $myrow['Mail_Street'];?>","<?php echo $myrow['Mail_City'];?>","<?php echo $myrow['Mail_Zip'];?>","2","<?php echo $myrow['Agent'];?>","TV","0","1","1","0","0","0.00","0.00","0","0","0","<?php echo $date;?>","P","","207","0","0","0","<?php echo $myrow['First_Name'];?>","<?php echo $myrow['Last_Name'];?>","Topa","1168.00","<?php echo $date;?>","A","0","<?php echo $myrow['Emp_Street'];?>","<?php echo $myrow['Emp_City'];?>","<?php echo $myrow['Emp_State'];?>","<?php echo $myrow['Emp_Zip'];?>","0.00","CA","207","1129.00","39.00","0.00","<?php echo $date;?>

<?php
}
?>

I know that line looks like jibberish, its just a hack job on a client file and I am using database data to fill it in...

Link to comment
https://forums.phpfreaks.com/topic/151953-export-to-program-help/
Share on other sites

Try this out. It uses the HEREDOC syntax to help with less confusion. I also removed all the extra going in and out of php and echos. This way you only have to echo once, and one echo is quicker than 20.

 

<?php
   $date=date("m,d,Y");
   $ID=$_GET['ID'];
   $result = mysql_query("SELECT * FROM clients WHERE ID = $ID"); 
   
   while($myrow = mysql_fetch_assoc($result)) {
	$output = '"0","0","0","0028836"\n';
	$output .=  <<<INFO
	"1","37","1","{$myrow['First_Name']} {$myrow['Middle_Name']} {$myrow['Last_Name']}","{$myrow['Phys_Street']}",
	"{$myrow['Phys_City']}","{$myrow['Phys_Zip']}","34","","35","{$myrow['Phone']}\",\"1\",\"2\",\"CA - FSC RATER\"
	,"{$date}","1641","32","{$myrow['Employer']}","{$myrow['Emp_Street']} {$myrow['Emp_City']} {$myrow['Emp_State']} {$myrow['Emp_Zip']}"
	,"","","{$myrow['Mail_Street']}","{$myrow['Mail_City']}","{$myrow['Mail_Zip']}","2","{$myrow['Agent']}"
	,"TV","0","1","1","0","0","0.00","0.00","0","0","0","{$date}" . '","P","","207","0","0","0"
	,"{$myrow['First_Name']}","{$myrow['Last_Name']}","Topa","1168.00","{$date}","A","0","{$myrow['Emp_Street']}","{$myrow['Emp_City']}"
	,"{$myrow['Emp_State']}","{$myrow['Emp_Zip']}","0.00","CA","207","1129.00","39.00","0.00","{$date}"
INFO;
}

   // unsure why this was in the loop...
    header("Content-Type: application/force-download");
header( "Content-Type:text/octet-stream" );
header('Content-disposition: attachment; filename="client.dat"');
   	
echo $output;
?>

 

Also moved the header items outside of the loop, I do not know why you put them in the loop in the first place.

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.