在php中使用模板数据创建一个新的CSV文件

时间:2021-09-01 06:36:50

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脚本,它将获取现有的csv文件,并使用该文件的值打开它进行编辑。

<?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.

那不是那么难。如果它变得毛茸茸,我还需要能够使用相同的模板数据添加其他行。我可以想一想如何做到这一点,根据用户选择的值进行某种计数,该值将呈现上述数据(加上换行符)x次。我只是不知道该怎么做。顺便说一句,有问题的csv文件有25列,我只是为了节省空间而放在前2列。

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

此外,一旦渲染了所有这些数据,我需要将其全部(所有行)保存到新的csv文件中。

1 个解决方案

#1


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

提供更加用户友好的输入方法会更容易吗?例如,包含与CSV文件匹配的列和行的表格?然后将每个单元格设为输入框。使用PHP magic name =“cell [0] [1]”确保以友好的数组获得结果。然后使用jQuery添加一行,如果他们愿意

$('#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.

然后遍历数组,再次将其另存为CSV。

#1


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

提供更加用户友好的输入方法会更容易吗?例如,包含与CSV文件匹配的列和行的表格?然后将每个单元格设为输入框。使用PHP magic name =“cell [0] [1]”确保以友好的数组获得结果。然后使用jQuery添加一行,如果他们愿意

$('#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.

然后遍历数组,再次将其另存为CSV。