Question

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>
Was it helpful?

Solution

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 } ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top