简体   繁体   English

使用PHP和HTML表编辑CSV数据

[英]Edit CSV data using PHP and HTML table

I have a CSV file that contains many data. 我有一个包含许多数据的CSV文件。

I use PHP to read and view it in WEB. 我使用PHP在WEB中阅读和查看它。 I need to have a functionality like ADD, EDIT and DELETE . 我需要具有ADD, EDIT and DELETE类的功能。 I already done the ADD using the a or append . 我已经使用aappend完成了ADD。 I will add data in HTML Table and automatically it will add also in CSV file. 我将在HTML表中添加数据,并自动将其也添加到CSV文件中。 I didn't use database here. 我没有在这里使用数据库。

My problem now is the EDIT. 我的问题现在是编辑。 What if i want to edit some data in CSV file using the HTML Table and like the ADD i want to automatically update it to my CSV file. 如果我想使用HTML表格编辑CSV文件中的某些数据,并且喜欢添加,我想自动将其更新为CSV文件,该怎么办? How can i do that? 我怎样才能做到这一点?

I already tried the w, w+, r+ but it didn't work. 我已经尝试过w, w+, r+但是没有用。 By the way I am using CODEIGNITER here. 顺便说一句,我在这里使用CODEIGNITER。

Thank you in advance for the help. 预先感谢您的帮助。

I would keep the logic on server and do all the csv rendering eventdriven. 我将逻辑保留在服务器上,并执行事件驱动的所有csv渲染。 Here you have a good csv lib: https://github.com/goodby/csv 在这里,您有一个不错的csv库: https : //github.com/goodby/csv

So the process would be something like this: 因此,过程将如下所示:

  • Client: Load csv preformated (via php) from server and display in html 客户端:从服务器加载经过格式化的csv(通过php)并以html显示
  • Client: Do MVVM bindings etc. 客户端:执行MVVM绑定等。
  • Client: If an event is called (like add, update etc.) send eventrequest to server 客户端:如果事件被调用(例如添加,更新等),则将事件请求发送到服务器
  • Server: Recieve event -> Process (with transactions if needed) -> reformat csv, save it and send back to client 服务器:接收事件->处理(如果需要,进行事务处理)->重新格式化csv,保存并发送回客户端
  • Client: Retrieve and apply MvvM bindings 客户端:检索并应用MvvM绑定

Otherwise you need redundant csv logic in client and server whats not a fine approach. 否则,在客户端和服务器中需要冗余的csv逻辑,这不是一个好的方法。 Do you know what I mean or should I do you an example? 您知道我的意思吗,还是应该给我一个榜样?

The csv library supports complex csv binding and easy appending like: csv库支持复杂的csv绑定并易于附加,例如:

$csv = new parseCSV();
$csv->parse('yourcsv.csv');
$csv->data[1] = array('updatekey' => 'new Value');
$csv->save();

Client side example: 客户端示例:

//retrieve your csv (view rendered or async with ajax
var csv;

var mutableDataArray = $.csv.toArray(csv);

//do manipulation here
//for example
mutableDataArray[0].name = 'A new name to first record';

//serialize back
var newCsv = "";
for(var i in mutableDataArray) {
    var data = mutableDataArray[i].join(",");
    newCsv += i < mutableDataArray[i].length ? data + "\n" : data;
}

//do something with newCsv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM