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 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程