Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating tab delimited file

Tags:

database

php

I am trying to create a tab delimited file output that I can use to load into quickbooks. I am using the following code on for the header.

header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=\"my-data.txt\"");

I have two problems.

The first is that some of the content is text but has a comma , in it (which is why I was using tab delimited. SQL (kindly) puts "" around those fields for me, which then get transfered to the text file and therfore subsequently into the accounts package. I cant see how I dissuade SQL from doing that or how to strip it out of the content of the text file.

Secondly, the first output in the script is an echo statement followed by a tab etc; The actual output however contains an extra row at the begining (I can see that an ASCII 0A has been inserted) I cant see where that is coming from?

I have attached the code I am using in case I am missing something obvious.

Help

Many thanks

<?php
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=\"my-data.txt\"");
// Open standard includes 
include ('./includes/protect.php');  //User name and password control
require_once('Connections/Dragonnet.php');
mysql_select_db($database_Dragonnet, $Dragonnet);  //Connect to database
?>

<?php

$batch1 =  $_GET["batch1"];
$batch2 =  $_GET["batch2"];
$query =  "SELECT invtrans.StdInvNar1 AS 'invtrans StdInvNar1',
                  invtrans.StdInvNar2 AS 'invtrans StdInvNar2',
                  invtrans.StdInvNar3 AS 'invtrans StdInvNar3',
                  invtrans.StdInvNar4 AS 'invtrans StdInvNar4',
                  invtrans.StdInvNar5 AS 'invtrans StdInvNar5',
                  invtrans.StdInvNar6 AS 'invtrans StdInvNar6',
                  invtrans.StdInvNar7 AS 'invtrans StdInvNar7',
                  invtrans.StdInvNar8 AS 'invtrans StdInvNar8',
                  invtrans.StdInvNar9 AS 'invtrans StdInvNar9',
                  invtrans.StdInvNar10 AS 'invtrans StdInvNar10',
                  invtrans.InvNar1 AS 'invtrans InvNar1',
                  invtrans.InvNar2 AS 'invtrans InvNar2',
                  invtrans.InvNar3 AS 'invtrans InvNar3',
                  invtrans.InvNar4 AS 'invtrans InvNar4',
                  invtrans.InvNar5 AS 'invtrans InvNar5',
                  invtrans.InvNar6 AS 'invtrans InvNar6',
                  invtrans.InvNar7 AS 'invtrans InvNar7',
                  invtrans.InvNar8 AS 'invtrans InvNar8',
                  invtrans.InvNar9 AS 'invtrans InvNar9',
                  invtrans.InvNar10 AS 'invtrans InvNar10',
                  invtrans.Charge AS 'invtrans Charge',
                  invtrans.Cost AS 'invtrans Cost',
                  invtrans.VATtype AS 'invtrans VATtype',
                  invtrans.VATRate AS 'invtrans VATRate',
                  invtrans.SiteName AS 'invtrans SiteName',
                  invtrans.SiteTown AS 'invtrans SiteTown',
                  invtrans.UnitPrice AS 'invtrans UnitPrice',
                  invtrans.Type AS 'invtrans Type',
                  invtrans.InvBatch AS 'invtrans InvBatch',
                  invoiceheaders.Name AS 'invoiceheaders Name',
                  invoiceheaders.Addr1 AS 'invoiceheaders Addr1',
                  invoiceheaders.Addr2 AS 'invoiceheaders Addr2',
                  invoiceheaders.Addr3 AS 'invoiceheaders Addr3',
                  invoiceheaders.Town AS 'invoiceheaders Town',
                  invoiceheaders.County AS 'invoiceheaders County',
                  invoiceheaders.Country AS 'invoiceheaders Country',
                  invoiceheaders.PostCode AS 'invoiceheaders PostCode',
                  invoiceheaders.EMail AS 'invoiceheaders EMail',
                  invoiceheaders.Fax AS 'invoiceheaders Fax',
                  invoiceheaders.InvNo AS 'invoiceheaders InvNo',
                  invoiceheaders.InvDate AS 'invoiceheaders InvDate',
                  invoiceheaders.InvType AS 'invoiceheaders InvType',
                  invoiceheaders.DelType AS 'invoiceheaders DelType'
FROM (invtrans invtrans
     INNER JOIN invoiceheaders invoiceheaders ON (invoiceheaders.InvNo = invtrans.InvNumber ))
WHERE
(
  invtrans.InvBatch = "."'".$batch1."'"." OR invtrans.InvBatch = "."'".$batch2."'"." )
ORDER BY  invoiceheaders.InvNo

";

$result = mysql_query($query) or die(mysql_error());

//Create Headers

    echo "Customer Ref";
    echo chr(9);
    echo "Account Ref";
    echo chr(9);
    echo "TxnDate";
    echo chr(9);
    echo "Invoice No";
    echo chr(9);
    echo "BillingAddress1";
    echo chr(9);
    echo "BillingAddress2";
    echo chr(9);
    echo "BillingAddress3";
    echo chr(9);
    echo "BillingAddressTown";
    echo chr(9);
    echo "BillingAddressCounty";
    echo chr(9);
    echo "BillingAddressCountry";
    echo chr(9);
    echo "BillingAddressPostcode";
    echo chr(9);
    echo "Phone";
    echo chr(9);
    echo "Fax";
    echo chr(9);
    echo "email";
    echo chr(9);
    echo "Description";
    echo chr(9);
    echo "Rate";
    echo chr(9);
    echo "UnitOfMeasure";
    echo chr(9);
    echo "SalesTaxCode";
    echo chr(9);
    echo "Amount";
    echo chr(9);
    echo chr(13);

while($row = mysql_fetch_array($result))

  {
    //create variables
    $description = $row['invtrans StdInvNar1']." ".$row['invtrans StdInvNar2']." ".$row['invtrans StdInvNar3']." ".$row['invtrans StdInvNar4']." ".$row['invtrans StdInvNar5']." ".$row['invtrans StdInvNar6']." ".$row['invtrans StdInvNar7']." ".$row['invtrans StdInvNar8']." ".$row['invtrans StdInvNar9']." ".$row['invtrans StdInvNar10'];
    $description = $description." ".$row['invtrans InvNar1']." ".$row['invtrans InvNar2']." ".$row['invtrans InvNar3']." ".$row['invtrans InvNar4']." ".$row['invtrans InvNar5']." ".$row['invtrans InvNar6']." ".$row['invtrans InvNar7']." ".$row['invtrans InvNar8']." ".$row['invtrans InvNar9']." ".$row['invtrans InvNar10'];


    //output file
    echo $row['invoiceheaders Name'];
    echo chr(9);
    echo "Account Ref";
    echo chr(9);
    echo $row['invoiceheaders InvDate'];
    echo chr(9);
    echo $row['invoiceheaders InvNo'];
    echo chr(9);
    echo $row['invoiceheaders Addr1'];
    echo chr(9);
    echo $row['invoiceheaders Addr2'];
    echo chr(9);
    echo $row['invoiceheaders Addr3'];
    echo chr(9);
    echo $row['invoiceheaders Town'];
    echo chr(9);
    echo $row['invoiceheaders County'];
    echo chr(9);
    echo $row['invoiceheaders Country'];
    echo chr(9);
    echo $row['invoiceheaders PostCode'];
    echo chr(9);
    echo "01212331234";
    echo chr(9);
    echo $row['invoiceheaders Fax'];
    echo chr(9);
    echo $row['invoiceheaders EMail'];
    echo chr(9);
    echo $description;
    echo chr(9);
    echo $row['invtrans Charge'];
    echo chr(9);
    echo "UnitOfMeasure";
    echo chr(9);
    echo "SalesTaxCode";
    echo chr(9);
    echo $row['invtrans Charge'];
    echo chr(9);
    echo chr(13);
  }

?>
like image 925
David Christy Avatar asked Dec 06 '22 16:12

David Christy


1 Answers

Use the built-in fputcsv() function with a "\t" separator ratherthan trying to "roll your own"

like image 139
Mark Baker Avatar answered Dec 10 '22 11:12

Mark Baker