tags:

views:

341

answers:

1

I am attempting to write a php script, that will take an existing csv file, and open it for edit using the values of that file.

<?PHP
$file_handle = fopen("test.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 100000);
echo "<textarea name=textarea id=textarea cols=40 rows=4>$line_of_text[0]</textarea>";
echo "<textarea name=textarea id=textarea cols=40 rows=4>$line_of_text[1]</textarea>";
echo "</br></br>";
}
fclose($file_handle);
?>

Thats not that difficult. Where it gets hairy, is i also need to be able to add additional rows using the same template data. I can think of how to do this, some kind of count based on a user selected value that would render the above data (plus a line break) x number of times. Im just not sure how to do that. By the way, the csv file in question has 25 columns, i just put in the first 2 for the sake of saving space.

Also, once all this data is rendered, i need to save it in its entirety (all rows) to a NEW csv file.

+2  A: 

Would it be easier to provide a more user friendly method of input? For example, a table with columns and rows which match your CSV file? Then make every cell an input box. Use the PHP magic name="cell[0][1]" to make sure you get the results in a friendly array. Then use jQuery to add a row if they wish

$('#add-row-button').bind('click', function() {

    var rowHtml = $('#csv-table tbody tr:first').html();

    $('#csv-table tbody tr:last').after(rowHtml);

{);

Then iterate through the array, saving it as a CSV again.

alex