doutui839638 2013-07-31 14:16
浏览 50
已采纳

合并来自fgetcsv数组的行

Basically I have a csv file with order information on it, ordered from a website, containing invoice number, name, address, etc. I've used PHP to turn this into invoices. It looks pretty good but I'm completely stuck on one bit.

Here's the opening bit using fgetcsv;

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

In the csv file, if someone has ordered more than one item it appears on a new row with the same invoice number as the row before, but doesn't show any product in the first row of the invoice number:

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",

The PHP takes these values (eg. $csvline[0] for the invoice number) and puts them in a nice printable html file. But I need to be able to get the rows beginning with the same invoice number onto the same html table (in the cell below the first ordered product).

Any suggestions for my pretty awfully worded problem would be very much appreciated!

Ps. Here's the php / html snippet:

<?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>
  • 写回答

1条回答 默认 最新

  • duanchu7271 2013-07-31 14:23
    关注

    WARNING Your csv is not normalized - so getting it to work will be nearly impossible with the current csv layout.

    Use the invoice number as the key to an array, then loop through the array in your output..

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

    Builds the array:

    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] => 
                    )
    
            )
    
    )
    

    Using this CSV(normalized)

    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",
    

    Also, I would recommend using tables for tabular data instead of divs

    To use this in your 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 } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀