Page 1 of 1

PHP Create CSV File

Posted: Thu May 17, 2007 3:56 pm
by fawadafr
Hellow All,

I am using PHP script to generate CSV file. For some reason when the script creates the CSV file it adds a quote - " - at the end of the first variable on the first column of the CSV file. I went line by line to see if I had an extra quote but could not fine any typos. Can some one help me if I am over looking at the script?

Code: Select all

<?
$header        = "header.html";
$footer        = "footer.html";
$returnpage    = "/";
$youremail     = "arashidi@implantdirect.com";
$csvfilename   = "FDA/FDA.csv";
$csvquote      = "\"\"";
$mode          = "BOTH";


//##############################################################
//#FUNCTION:   doFormError                                     #
//#RETURNS:                                                    #
//#PARAMETERS: A error message string.                         #
//#PURPOSE:    Generates an HTML page indicating a form        #
//#            submission error occurred.                      #
//##############################################################
function doFormError($errString) {

    include($header);

    echo "<FONT SIZE=+2>The form you submitted was not complete.<BR><BR></FONT>";
    echo "$errString<BR><BR>\n";
    echo "<INPUT TYPE=BUTTON ONCLICK='history.back()' VALUE='  Return to the form '><HR>";

    include($footer);

    exit;
}

//##############################################################
//#FUNCTION:   doError                                         #
//#RETURNS:                                                    #
//#PARAMETERS: A error message string.                         #
//#PURPOSE:    Generates an HTML page indicating an error      #
//#            occurred.                                       #
//##############################################################
function doError($errString) {

    include($header);

    echo "$errString<BR><BR>\n";

    include($footer);

    exit;
}



//##############################################################
//##############################################################
//###  MAIN                                                  ###
//##############################################################
//##############################################################

if (($myfirst == "") || ($lName == "") || ($email == "") || ($pNumber1 == "") || ($pNumber2 == "") || ($pNumber3 == "")) {
   doFormError("I'm sorry, but it appears that you have forgotten to fill the required field(s). Please go <A HREF='Javascript:history.go(-1);'>back</A> and correct the error(s).");
   exit;
}

//# checks for valid email address
if( !(ereg("^(.+)@(.+)\\.(.+)$",$email)) ) {
    doFormError("I am sorry, You have submitted an invalid e-mail address. In order to get a confirmation e-mail you must provide a valid e-mail address.
	Please go <A HREF='Javascript:history.go(-1);'>back</A> and correct the error.");
    exit;
}

$today = date ("l, F jS Y");
$strMessageBody = "";
$strMessageBody .= "\n";
$strMessageBody .= "Date: $today \n";
$strMessageBody .= " \n";
$strMessageBody .= "Here is the information you have submitted: \n";
$strMessageBody .= "-------- \n";
$strMessageBody .= "A. Originator Information: \n";
$strMessageBody .= "   1. First Name: \t $myfirst \n";
$strMessageBody .= "   2. Last Name: \t $lName \n";
$strMessageBody .= "   3. Title/Organization: $title \n";
$strMessageBody .= "   4. E-Mail Address: \t $email \n";
$strMessageBody .= "   5. Phone Number: \t ($pNumber1) $pNumber2 - $pNumber3 \n";
$strMessageBody .= "   6. Fax Number: \t ($fNumber1) $fNumber2 - $fNumber3 \n";
$strMessageBody .= "   7. Street Address: \t $sAddress \n";
$strMessageBody .= "   8. City: \t $city \n";
$strMessageBody .= "   9. State/Province: \t $state \n";
$strMessageBody .= "  10. Postal Code: \t $zipCode \n";
$strMessageBody .= "  11. Country: \t $country \n";
$strMessageBody .= "\n";
$strMessageBody .= "B. Patient Information: \n";
$strMessageBody .= "   1. Date of Birth: \t $date1 \n";
$strMessageBody .= "   2. Sex: \t $sex \n";
$strMessageBody .= "   3. Weight: \t $weight $weightUnit \n";
$strMessageBody .= "\n";
$strMessageBody .= "C. Product Report: \n";
$strMessageBody .= "   1. $clinic $product \n";
$strMessageBody .= "   2. Date of Event: \t $date2 \n";
$strMessageBody .= "   3. Describe Event or Problem: \t $C3 \n";
$strMessageBody .= "   4. Other relevant information to consider, including other preexisting conditions: $OtherRelevantHistory \n";
$strMessageBody .= "   5. Other Relevant Information to consider, Including Preexisting Conditions: \n";
$strMessageBody .= "\n";
$strMessageBody .= "D. Product Information: \n";
$strMessageBody .= "   1. Device Name: \t $maker \n";
$strMessageBody .= "   1.1 Device Type: \t $typer \n";
$strMessageBody .= "   1.2 Device Model: \t $modelr \n";
$strMessageBody .= "   1.3 Lot Number: \t $lotNumber1 \n";
$strMessageBody .= "\n";
$strMessageBody .= "   2. Device Name: \t $makerr \n";
$strMessageBody .= "   2.1 Device Type: \t $typerr \n";
$strMessageBody .= "   2.2 Device Model: \t $modelrr \n";
$strMessageBody .= "   2.3 Lot Number: \t $lotNumber2 \n";
$strMessageBody .= "\n";
$strMessageBody .= "   3. Device Name: \t $makerrr \n";
$strMessageBody .= "   3.1 Device Type: \t $typerrr \n";
$strMessageBody .= "   3.2 Device Model: \t $modelrrr \n";
$strMessageBody .= "   3.3 Lot Number: \t $lotNumber3 \n";
$strMessageBody .= "\n";
$strMessageBody .= "   4. Device Available for Evaluation: $available \n";
$strMessageBody .= "   5. If Implanted, Give Date: $date3 \n";
$strMessageBody .= "   6. If Explanted, Give Date: $date4 \n";
$strMessageBody .= "   7. Please list additional devices as necessary: $additionalInfo \n";

$strMessageBody .= "\n";
$strMessageBody .= "E. Upload Files: \n";
$strMessageBody .= "   1. File Name(s): \t $Upload";



//if( $NAME_2 ) {$strMessageBody .= "$QUANTITY_2    \$$PRICE_2    $ID_2 - $NAME_2   $ADDTLINFO_2  \n";}


if( $mode == "BOTH" || $mode == "EMAIL") {
   //# Send email order to Ahmad Rashidi...
   $mailheaders = "From: $email\r\n";
   $subject = "Customer Complaint / Feed Back Form";
   mail($youremail, $subject, $strMessageBody, $mailheaders);
}


if( $mode == "BOTH" || $mode == "FILE") {
   
   //$csvcomments = $comment;
   if (!$CSVF = fopen($csvfilename,'a')) {
       doError("Unable to connect to the database. Your information has not been saved to our datebase!");
       exit;
   }

   //fputs($CSVF, $string);
   //fputs($CSVF, "\",\"");
   fputs($CSVF, "$myfirst");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$lName");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$title");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$email");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$pNumber1-$pNumber2-$pNumber3");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$fNumber1-$fNumber2-$fNumber3");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$sAddress");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$city");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$state");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$zipCode");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$country");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$date1");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$sex");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$weight");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$weightUnit");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$clinic");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$product");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$date2");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$C3");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$C4");
   fputs($CSVF, "\",\""); 
   fputs($CSVF, "$C5");
   fputs($CSVF, "\",\"");   
   fputs($CSVF, "$maker");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$typer");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$modelr");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$lotNumber1");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$makerr");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$typerr");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$modelrr");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$lotNumber2");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$makerrr");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$typerrr");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$modelrrr");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$lotNumber3");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$available");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$date3");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$date4");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$additionalInfo");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$datefield");
   //Sameer's balnk fields:
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0001");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0002");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0003");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0004");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0005");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0006");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0007");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0008");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0009");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0010");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0011");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0012");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0013");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0014");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0015");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0016");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0017");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0018");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0019");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0020");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0021");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0022");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0023");
   fputs($CSVF, "\",\"");
   fputs($CSVF, "$blank_0024");
   //end of the blank fields
   fputs($CSVF, "\"\n");
   fclose($CSVF);
}

//# Send email conformation to the customer.....
$mailheaders = "From: $youremail\r\n";
$subject = "Implant Direct LLC";
mail($email, $subject, $strMessageBody, $mailheaders);

include($header);

echo "<h2>Customer Complain / Feed Back Form</h2>";
echo "Thank you for contacting Implant Direct LLC. You will receive a confirmation e-mail from us ";
echo "momentarily. Please contact us at <a href=\"mailto:arashidi@implantdirect.com\">XXXX</a> if you have any questions or concerns.";
echo "<P>";
//echo "<A HREF=\"$returnpage\" target=_top>Return Home</A>";
echo "<P>";

include($footer);

?>

Results

Posted: Thu May 17, 2007 3:59 pm
by fawadafr
Here is the out put of the CSV file:

Code: Select all

Ahmad"	Rashidi	Implant Direct LLC	arashidi@implantdirect.com	818-444-3316	818-444-3300	27030 Malibu Hills Rd	Calabasas	CA	91301	United States 	5/1/1983	Male	185	LBS	Clinical Event		3/28/2006	Here i need to describe the event or problem	Vascular Disorders, Clotting Disorders	Do I have other information	Implant	ScrewPlus	4.7mmDx13mmL (204713)	123456	Prosthetics	ScrewPlus	2mm Collar Height (1037-62-B)	654987	Surgical Instruments	Surgical Trays	Surgical Tray-Basic Kit	8285825	Yes	9/29/1983	3/27/1982	list additional devices as necessary	5/17/2007

Posted: Thu May 17, 2007 6:08 pm
by RobertGonzalez
Try this insted of all those quotes:

Code: Select all

<?php
fputs($CSVF, $myfirst);
fputs($CSVF, '","');
fputs($CSVF, $lName);
fputs($CSVF, '","');
// and so on throught the data
fputs($CSVF, "\n");
fclose($CSVF); 
?>

Posted: Thu May 17, 2007 8:23 pm
by feyd
fputcsv()? (PHP 4 alternates are posted on the page too..)

Posted: Thu May 17, 2007 8:26 pm
by califdon
There's a FAR easier way to output a csv file. No need to fopen() and puts(). See if this wouldn't serve your purpose:

Code: Select all

<?php
// Here we tell the browser that this is a csv file.
header("Content-type: application/octet-stream"); 
header("Content-disposition: attachment; filename=email-no-address-from-db-".date("Y-m-d").".csv");
header("Pragma: no-cache"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Expires: 0"); 
mysql_connect('localhost','electraweb','xxxxx');
mysql_select_db('electra') or die("Unable to select database. Try again later.");
$sql="SELECT lastname,firstname,email FROM persons WHERE email<>'' AND address1=''";
$result=mysql_query($sql) or die("Query failed.<br>$sql");
$row=mysql_fetch_assoc($result);
while ($row) {
	extract($row);
	echo "$email,$lastname,$firstname\n";
	$row=mysql_fetch_assoc($result);
}
?>
Of course, you would substitute your variables and formatting. The first header statement MUST be the first data sent to the browser (you can't send a doctype or html tag before you send the first header).

Instead of sending the file as an email attachment, this just delivers the file to the browser, where the user can then save it or open it with Excel, or whatever.

If you really must send it as an email attachment, this won't do what you want, but it's so simple that it's worth considering.

A couple of references:

http://www.expertsrt.com/tutorials/Matt ... aders.html
http://www.plus2net.com/php_tutorial/ph ... wnload.php

Posted: Fri May 18, 2007 2:30 pm
by fawadafr
Thank for your respond,

I changed my source code according to your suggestion

Code: Select all

fputs($CSVF, $myfirst);
   fputs($CSVF, '","');
   fputs($CSVF, $lName);
   fputs($CSVF, '","');
   fputs($CSVF, $title);
   fputs($CSVF, '","');
again, when the CSV file is created I get a " - quote - at the end of the first variable.

Please let me know if you have any other suggestions,

Thanks

Posted: Fri May 18, 2007 2:57 pm
by RobertGonzalez
Echo out the variables to make sure that the var itself is not responsible for doing that.

Posted: Fri May 18, 2007 3:45 pm
by fawadafr
Thanks Everah,

Thanks for your help, unfortunately, I am new to PHP scripting. What does "Echo out" mean? I used the same variable for the first and second column of my CSV file. On the first column I got the value of the variable with the annoying quote but on the second column the value of the variable was passed without any quote.

I went through the function see if there is any type or loop causing this but could not fine any? I have no idea how to get red of this!

One again thank for your fast respond!

Posted: Fri May 18, 2007 4:08 pm
by RobertGonzalez
do this:

Code: Select all

<?php
echo "$myfirst, $lName, $title\n";
?>
And see what that puts on the screen. Then do a view source and see what is showing up in the output HTML. Post both back.

Posted: Fri May 18, 2007 4:29 pm
by fawadafr
Thanks,
So you are suggesting that I should seprate each variable by comma. So, it would look some thing like this:

Code: Select all

fputs($myfirst,$lName,$title,$email,$pNumber1-$pNumber2-$pNumber3,$fNumber1-$fNumber2-$fNumber3,$sAddress,$city,$state,$zipCode,$country);
I am sorry if I misunderstood your suggestions. If I just use your statement it just doesn't do anything. I mean it doesn't create the CSV file.

Thanks,

Posted: Fri May 18, 2007 5:18 pm
by RobertGonzalez
No, what I am saying right now is to echo out the variables like I showed you in my last post, just so you can see what is coming out of them. Before we get into putting the data into your file I want to see what the application is seeing in your data.