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
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.