doupo1865 2014-03-21 06:38
浏览 40
已采纳

使用PHP和HTML表编辑CSV数据

I have a CSV file that contains many data.

I use PHP to read and view it in WEB. I need to have a functionality like ADD, EDIT and DELETE. I already done the ADD using the a or append. I will add data in HTML Table and automatically it will add also in CSV file. 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. How can i do that?

I already tried the w, w+, r+ but it didn't work. By the way I am using CODEIGNITER here.

Thank you in advance for the help.

  • 写回答

1条回答 默认 最新

  • du6jws6975 2014-03-21 07:06
    关注

    I would keep the logic on server and do all the csv rendering eventdriven. Here you have a good csv lib: 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
    • Client: Do MVVM bindings etc.
    • 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
    • Client: Retrieve and apply MvvM bindings

    Otherwise you need redundant csv logic in client and server whats not a fine approach. 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 = 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 + "
    " : data;
    }
    
    //do something with newCsv
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?