I’ve a php script which loads basically just one csv via:
echo json_encode(file(csvfile.csv, FILE_IGNORE_NEW_LINES));
The output is something like this:
[“foo,bar,baz”,”foo, foo,bar”,”bla,bla,blubb”]
In my basic html file I load it in this way:
$(document).ready(function(){
$.getJSON("curlcsvfetcher.php", function(data) {
$('#data').empty();
var columnseperator = "|";
var commentindicator = "#";
var textqualifier = '"'
filltable2(data,columnseperator,commentindicator,textqualifier); }
);
This works great with the function filltable() but not with filltable2():
function filltable(data,columnseperator,commentindicator,textqualifier){
var table_obj = $('table');
$.each(data, function(index, item){
table_obj.append($('<tr id="'+item+'"><td>'+item+'</td></tr>'));
}).insertAfter('.table');
}
function filltable2(data,columnseperator,commentindicator,textqualifier){
var table_obj = $('table');
var trstart = '<tr>';
var trend = '</tr>';
$.each(data, function(index, item){
table_obj.append($('<tr id="'+item+'"><td>'+item+'</td></tr>'));
var columns = data.split(columnseperator);
var tddata = '';
for (var column in columns){
tddata = tddata +'<td>'+column+'</td>';
}
var rowdata = trstart + tddata + trend;
table_obj.append($(rowdata));
}).insertAfter('.table');
}
The function filltable() generate the output with one column and 3 rows.
But now I want to explode each line in (this case 3) columns.
There must be a problem with my code in function filltable2() and I don’t know what I am doing wrong.
The output of filltable2() is just one line in the table.
Thank you very much!
PS: This problem is related to my last question but I think the basic problem is unrelated, so i created a new question
I think it’s because in the split line, you’re giving it:
When you mean
or