php - combine rows from fgetcsv arrays -
basically have csv file order information on it, ordered website, containing invoice number, name, address, etc. i've used php turn invoices. looks pretty i'm stuck on 1 bit.
here's opening bit using fgetcsv;
<?php $file_handle = fopen("orders.csv", "r"); while (!feof($file_handle) ) { $csvline = fgetcsv($file_handle, 1024); ?>
in csv file, if has ordered more 1 item appears on new row same invoice number row before, doesn't show product in first row of 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] invoice number) , puts them in nice printable html file. need able rows beginning same invoice number onto same html table (in cell below first ordered product).
any suggestions pretty awfully worded problem appreciated!
ps. here's 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">£<?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">£<?php echo $csvline[20]; ?></div> </div> <div class="odd"> <div class="t2 td">more product lines??????????????????</div> <div class="t3 td">£amount</div> <div class="t4 td">quantity</div> <div class="t5 td">vat</div> <div class="t6 td">£total amount</div>
warning csv not normalized - getting work impossible current csv layout.
use invoice number key
array
, loop through array
in 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 array:
array ( [100001] => array ( [0] => array ( [0] => 100001 [1] => bradley manning [2] => email1@address.com [3] => address [4] => product [5] => ) ) [100002] => array ( [0] => array ( [0] => 100002 [1] => mr snowden [2] => email2@address.com [3] => address [4] => product [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 [5] => ) [2] => array ( [0] => 100003 [1] => [2] => [3] => [4] => product b [5] => ) [3] => array ( [0] => 100003 [1] => [2] => [3] => [4] => product c [5] => ) ) )
using 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, recommend using tables
tabular data instead of divs
to use in html
<?php foreach($orders $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 $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 } ?>
Comments
Post a Comment