0

基本上我有一个带有订单信息的 csv 文件,从网站订购,包含发票号码、姓名、地址等。我使用 PHP 将其转换为发票。它看起来不错,但我完全坚持了一点。

这是使用 fgetcsv 的开始位;

<?PHP
$file_handle = fopen("orders.csv", "r");
while (!feof($file_handle) ) {
$csvline = fgetcsv($file_handle, 1024);
?>

在 csv 文件中,如果有人订购了多个商品,它会出现在新行中,发票编号与前一行相同,但发票编号的第一行中不显示任何产品:

100001,Bradley Manning,email1@address.com,Address,Product A,
100002,Mr Snowden,email2@address.com,Address,Product A,
100003,Dr Dre,email3@address.com,Address,,,
100003,,,,,,,,,,"Product A",
100003,,,,,,,,,,"Product B",
100003,,,,,,,,,"Product C",

PHP 采用这些值(例如,发票编号为 $csvline[0])并将它们放入一个漂亮的可打印 html 文件中。但我需要能够将以相同发票号开头的行放到同一个 html 表中(在第一个订购产品下方的单元格中)。

对于我措辞非常糟糕的问题的任何建议将不胜感激!

附言。这是 php / html 片段:

<?php
$exvat = ($csvline[15] / 1.2);
$exvatrounded = number_format($exvat, 2, '.', '');
?>
<div class="t2 th">Description</div>
<div class="t3 th">Unit Price</div>
<div class="t4 th">Quantity</div>
<div class="t5 th">VAT Rate</div>
<div class="t6 th">Amount</div>
<div class="even">
<div class="t2 td"><?php echo $csvline[12]; ?></div>
<div class="t3 td">&pound;<?php echo $exvatrounded; ?></div>
<div class="t4 td"><?php echo $csvline[14]; ?></div>
<div class="t5 td"><?php echo $csvline[16]; ?></div>
<div class="t6 td">&pound;<?php echo $csvline[20]; ?></div>
</div>
<div class="odd">
<div class="t2 td">MORE PRODUCT LINES??????????????????</div>
<div class="t3 td">&pound;AMOUNT</div>
<div class="t4 td">QUANTITY</div>
<div class="t5 td">VAT</div>
<div class="t6 td">&pound;TOTAL AMOUNT</div>
4

1 回答 1

1

警告您的 csv 未标准化 - 因此使用当前的 csv 布局几乎不可能使其正常工作。

使用发票编号作为keyarray,然后在输出中循环array..

$file_handle = fopen("orders.csv", "r");
$orders = array();
while (!feof($file_handle) ) {
    $csvline = fgetcsv($file_handle, 1024);
    $orders[$csvline[0]][] = $csvline;
}
//print_r($orders);

构建数组:

Array
(
    [100001] => Array
        (
            [0] => Array
                (
                    [0] => 100001
                    [1] => Bradley Manning
                    [2] => email1@address.com
                    [3] => Address
                    [4] => Product A
                    [5] => 
                )

        )

    [100002] => Array
        (
            [0] => Array
                (
                    [0] => 100002
                    [1] => Mr Snowden
                    [2] => email2@address.com
                    [3] => Address
                    [4] => Product A
                    [5] => 
                )

        )

    [100003] => Array
        (
            [0] => Array
                (
                    [0] => 100003
                    [1] => Dr Dre
                    [2] => email3@address.com
                    [3] => Address
                    [4] => 
                    [5] => 
                    [6] => 
                )

            [1] => Array
                (
                    [0] => 100003
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => Product A
                    [5] => 
                )

            [2] => Array
                (
                    [0] => 100003
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => Product B
                    [5] => 
                )

            [3] => Array
                (
                    [0] => 100003
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => Product C
                    [5] => 
                )

        )

)

使用这个 CSV(标准化)

100001,Bradley Manning,email1@address.com,Address,Product A,
100002,Mr Snowden,email2@address.com,Address,Product A,
100003,Dr Dre,email3@address.com,Address,,,
100003,,,,"Product A",
100003,,,,"Product B",
100003,,,,"Product C",

另外,我建议使用tables表格数据而不是divs

要在您的HTML

<?php foreach($orders as $order_id => $products) { ?> 
    <div class="t2 th">Description</div>
    <div class="t3 th">Unit Price</div>
    <div class="t4 th">Quantity</div>
    <div class="t5 th">VAT Rate</div>
    <div class="t6 th">Amount</div>
    <?php foreach($products as $product) { ?> 
    <div class="even">
        <div class="t2 td"><?php echo $product[4] ?></div>
        <div class="t3 td">&pound;</div>
        <div class="t4 td"></div>
        <div class="t5 td"></div>
        <div class="t6 td"></div>
    </div>
    <?php } ?>
<?php } ?>
于 2013-07-31T14:23:22.200 回答