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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)