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">£</div>
<div class="t4 td"></div>
<div class="t5 td"></div>
<div class="t6 td"></div>
</div>
<?php } ?>
<?php } ?>