简体   繁体   English

在php中将产品订单写入txt文件

[英]Writing product orders to txt file in php

I'm working on some code for an online store to write orders to a txt file once they have been placed. 我正在为在线商店编写一些代码,以便在放置txt文件后将订单写入。

To generate this string I am using the following code... However it is only writing the information for the very last item in the array. 要生成此字符串,我使用以下代码...但是它只是为数组中的最后一项写入信息。 I know that this is because as it runs through the foreach loop it is discarding previous information but have not been able to think of a way to counter this. 我知道这是因为它在foreach循环中运行时它丢弃了以前的信息但却无法想出解决这个问题的方法。

Any suggestions of a different way to tackle this? 有什么不同的方法可以解决这个问题吗?

foreach($_SESSION['itemname'] as $key=>$value) {
    $output_products =""
    ."item: ". $_SESSION['itemname'][$key] ."\t"
    ."qty: ".$_SESSION['itemqty'][$key]."\t"
    ."price: ".$_SESSION['itemprice'][$key]."\t"
    ."Sub Total: ".$_SESSION['subtotal'][$key]."\t";
}
$output_string = ""
.$_SESSION['fname'] ."\t"
.$_SESSION['lname'] ."\t"
.$_SESSION['address'] ."\t"
.$output_products ."\t"
.$_SESSION['grandTotal']."\n";

change 更改

foreach($_SESSION['itemname'] as $key=>$value) {
    $output_products =""
    ."item: ". $_SESSION['itemname'][$key] ."\t"
    ."qty: ".$_SESSION['itemqty'][$key]."\t"
    ."price: ".$_SESSION['itemprice'][$key]."\t"
    ."Sub Total: ".$_SESSION['subtotal'][$key]."\t";
}

to

$output_products ="";
foreach($_SESSION['itemname'] as $key=>$value) {
    $output_products .=""
    ."item: ". $_SESSION['itemname'][$key] ."\t"
    ."qty: ".$_SESSION['itemqty'][$key]."\t"
    ."price: ".$_SESSION['itemprice'][$key]."\t"
    ."Sub Total: ".$_SESSION['subtotal'][$key]."\t";
}

You can use .= for appending strings. 您可以使用.=附加字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM