我正在尝试创建一个制表符分隔文件输出,我可以用它来加载到quickbooks中。我正在使用以下代码作为标题。
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=\"my-data.txt\"");
我有两个问题。
首先是一些内容是文本,但其中有一个逗号,
(这就是我使用制表符分隔的原因.SQL(善意)放置""围绕这些字段对我来说,然后转移到文本文件,然后进入帐户包。我无法看到我如何阻止SQL这样做或如何从文本文件的内容中删除它。
其次,脚本中的第一个输出是一个echo语句,后跟一个tab等;然而,实际输出在开头包含一个额外的行(我可以看到已经插入了ASCII 0A)我无法看到它来自哪里?
我附上了我正在使用的代码,以防我遗漏了一些明显的东西。
帮助
非常感谢
<?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);
}
?>
答案 0 :(得分:10)
使用带有“\ t”分隔符的内置fputcsv()功能,而不是尝试“自己滚动”
答案 1 :(得分:2)
首先,制表符分隔文件的效果非常好。我几乎每天都使用它们。它们比逗号分隔文件更容易使用,因为很多数据都有注释(例如姓氏,名字和街道地址)。
您在没有意识到的情况下自己插入换行符。
此标记?>
到此标记?php
的所有内容都会发送到浏览器。这是你的换行符。
至于额外的引号,请使用trim语句。
echo trim($row['invtrans Charge']);
答案 2 :(得分:1)
不同的字符集以不同方式读取标签。另外,标签可以在空格(以及空格数[3/4])和标签之间切换,具体取决于用于创建它们的IDE。
你需要选择一个不同的分隔符...我可以建议一个管道&#39; |&#39;因为它不会在英语或HTML中使用,因此它可能会被拾取并作为一个特殊字符进行清理。