Jump to content

CSV Creation headaches


texmansru47

Recommended Posts

All,

 

I have been struggling to create a very simple CSV file (a physical file) from php.  I have tried several variations, but here is what I have currently:

 

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$newline = "<br />";
$space =" ";
if (isset($_GET['param1']) && !empty($_GET['param1'])){
    $BatchNumData=$_GET['param1'];
    echo "The Batch Number is:\n$BatchNumData and the Shipment is listed as Good.".$newline.$newline; }
    elseif ($_SERVER['REQUEST_METHOD'] == 'POST'){    
    $BatchNumData=$_POST['param1'];
    echo "The Batch Number is:\n$BatchNumData".$newline.$newline;         
// Connect to mysql database    
$con = mysql_connect("localhost","user","XXXXXX") or die('Connection: ' . mysql_error());; 
mysql_select_db("datatest", $con) or die('Database: ' . mysql_error());  
//************************************************************
//**        Create CSV file for Labeling MP                 **
//************************************************************ 
$mosConfig_locale_debug = 0;
$mosConfig_locale_use_gettext = 0;
$mpit=$_POST['MPID'];
$sql = "SELECT * FROM `ShipTemp` WHERE `BatchNum` = '$BatchNumData' AND `MPBoxNum` = '$mpit'";
$results = mysql_query($sql);
$sernumi=$results['serNum'];
$model=$results['OEMSku'];
$masterid=$results['MPBoxNum'];
$_file = 'optlbl1.csv'; 
$_fp = @fopen( $_file, 'w' ); 

while (list($sernum,$model,$masterid)=mysql_fetch_row($results)) 
    { 
    $_csv_data=$imei.','.$model.','.$masterid. "\n"; 
    @fwrite( $_fp, $_csv_data ); 
    } 
    @fclose( $_fp ); 

mysql_close($con);
}        
?>

 

and I get NOTHING for output.  I know I'm missing something, but what?  There are NO GOOD tutorials out there and please do not suggest php.net, since I have been all over that and nothing there is clear enough to figure out the root cause of why my codes are not working.

 

Any ideas?

 

 

Link to comment
https://forums.phpfreaks.com/topic/130308-csv-creation-headaches/
Share on other sites

You mean the following:

 


$_fp = @fopen( $_file, 'w' );   // should be $_fp = fopen( $_file, 'w' ); 
@fwrite( $_fp, $_csv_data );  // should be fwrite( $_fp, $_csv_data ); 
@fclose( $_fp );  // should be fclose( $_fp ); 

 

Would that be correct?

 

oh...you want it to prompt for download? the script you posted will save it to a file where the script is. try this for having it prompt for download:

 

replace this:

$_file = 'optlbl1.csv'; 
$_fp = @fopen( $_file, 'w' ); 

while (list($sernum,$model,$masterid)=mysql_fetch_row($results)) 
    { 
    $_csv_data=$imei.','.$model.','.$masterid. "\n"; 
    @fwrite( $_fp, $_csv_data ); 
    } 
    @fclose( $_fp ); 

with

$_file = 'optlbl1.csv'; 
$_contents = "";

while (list($sernum,$model,$masterid)=mysql_fetch_row($results))
{ 
    $_contents .= $imei.','.$model.','.$masterid. "\n"; 
} 

header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" );
header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" );
header ( "Pragma: no-cache" );
header ( "Content-type: text/x-csv" );
header ( "Content-Disposition: attachment; filename={$_file}" );
header ( "Content-Length: ". strlen($contents) ); 
echo $contents;
exit;

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.