I am importing a csv file which has a lot of invoice data. This data needs to be grouped together based on Vendor ID to display a heading for each Vendor with a sum of all invoices for that Vendor Id; and also then display each individual invoice below it. The goal of this project is to import the CSV, display the data to the user, allow for certain values to be changed via select boxes, and then at a click of a button export to XML. The XML is structured in a similar way as the HTML where by each group of invoices has certain common data displayed at first and then the remittence data is below. To achieve this, I am trying to structure the Object in a way to help me convert it properly to XML using this http://goessner.net/download/prj/jsonxml/ .
Problem: The second two payments need to be grouped together and I would like an array to be created for each row of detailed data and then be added to the main object into the PmtDetail attribute. At the moment this code only ads the first row and ignores the rest.
CSV Snippet (I am not including all the rows that are being used in the code here)
ID CardCode payment_sum amount
1610165 BENV5271 100 100
1609026 BENV5635 509.85 287.33
1609025 BENV5635 509.85 222.52
JSON
[{"DocNum":"1610165","CardCode":"BENV5271","InvPayAmnt":"100.00","PmntDate":"2012-03-29","payment_sum":"100.00"},
{"DocNum":"1609026","CardCode":"BENV5635","InvPayAmnt":"287.33","PmntDate":"2012-03-29","payment_sum":"509.85"},
{"DocNum":"1609025","CardCode":"BENV5635","InvPayAmnt":"222.52","PmntDate":"2012-03-29","payment_sum":"509.85"}]
Jquery
$(document).ready(function() {
$.getJSON('CSV.php', function(data) {
var prevCardCode = '';
var newDiv; var NewDiv2;
var PaymentFormat;
$.each(data, function(index, element) { //looping once to display header info such as sum
XMLObject = []; // Creating a new object for each row
if (element['CardCode'] != prevCardCode) {
XMLObject.Vendor_Sum = element['payment_sum'];
XMLObject.Name1 = element['CardName'];
XMLObject.Addr1 = element['Address'];
console.log(XMLObject);
newDiv = $('<div/>').addClass('row').appendTo('#showdata');
$('<div class="sum_field">' + 'Total: ' + element['payment_sum'] + '</div>').appendTo(newDiv);
$('<div class="options">Payment Format: <select name="Payment_Format" id="Payment_Format"><option value="CTX" selected="selected">Company to Company</option><option value="PPD">Company to Person</option></select> </div><div id="Selected_Format"></div>').appendTo(newDiv);
XMLObject.paymentFormat = $('select#Payment_Format').val();;
$('select#Payment_Format').change(function(){
PaymentFormat = $(this).val();
XMLObject.paymentFormat = PaymentFormat;
});
}
newDiv2 = $('<div/>').addClass('sub_row').appendTo(newDiv);
prevCardCode = element['CardCode'];
$.each(element, function(key, value) { looping 2nd time to display the detail info
XMLObjectDetail = {}; // Creating an array for each row of detail info
XMLObjectDetail['TotalCurAmount'] = element['InvPayAmnt'];
XMLObjectDetail['NetCurAmount'] = element['InvPayAmnt'];
$('<div class="field">' + value + '</div>').appendTo(newDiv2);
XMLObject.PmtDetail = XMLObjectDetail;
});
});
});
});
PHP
<?php
if (($handle = fopen('upload/BEN-new.csv', "r")) === FALSE) {
die('Error opening file');
}
$headers = fgetcsv($handle, 1024, ',');
$complete = array();
while ($row = fgetcsv($handle, 1024, ",")) {
$complete[] = array_combine($headers, $row);
}
fclose($handle);
echo json_encode($complete);
?>
=should be:$.extend({}, value);console.logthe whole array, orconsole.logeach element within the$.eachusingconsole.log(key,value)i don't think you need$.extend