【问题标题】:update table html and save to csv更新表格 html 并保存到 csv
【发布时间】:2025-12-28 03:25:41
【问题描述】:

我是 php-codeigniter 的初学者

如何更新来自 CSV 文件的数据的现有数据。

例如,我想编辑第 3 行并保存它。当我打开 CSV 文件时,我在 web 表 html 中编辑的行应该更新 CSV 文件,所以当我打开 CSV 文件时它应该已经存在。这里不需要数据库。

请帮帮我,谢谢。

下面是我的编辑代码,当您单击它时,文本框将显示在所有受影响的行中。在我看来,这是我的 javascript

查看

function Edit(){
            var par = $(this).parent().parent(); //tr
            var f1 = par.children("td:nth-child(1)");
            var f2 = par.children("td:nth-child(2)");
            var f3 = par.children("td:nth-child(3)");
            var f4 = par.children("td:nth-child(4)");
            var f5 = par.children("td:nth-child(5)");
            var f6 = par.children("td:nth-child(6)");
            var f7 = par.children("td:nth-child(7)");
            var f8 = par.children("td:nth-child(8)");
            var f9 = par.children("td:nth-child(9)");
            var f10 = par.children("td:nth-child(10)");
            var f11 = par.children("td:nth-child(11)");
            var f12 = par.children("td:nth-child(12)");
            var f13= par.children("td:nth-child(13)");
            var f14 = par.children("td:nth-child(14)");
            var f15 = par.children("td:nth-child(15)");
            var f16 = par.children("td:nth-child(16)");
            var f17 = par.children("td:nth-child(17)");

            f1.html("<input type='text' name='f1' id='f1' value='"+f1.html()+"'/>");
            f2.html("<input type='text' name='f2' id='f2' value='"+f2.html()+"'/>");
            f3.html("<input type='text' name='f3' id='f3' value='"+f3.html()+"'/>");
            f4.html("<input type='text' name='f4' id='f4' value='"+f4.html()+"'/>");
            f5.html("<input type='text' name='f5' id='f5' value='"+f5.html()+"'/>");
            f6.html("<input type='text' name='f6' id='f6' value='"+f6.html()+"'/>");
            f7.html("<input type='text' name='f7' id='f7' value='"+f7.html()+"'/>");
            f8.html("<input type='text' name='f8' id='f8' value='"+f8.html()+"'/>");
            f9.html("<input type='text' name='f9' id='f9' value='"+f9.html()+"'/>");
            f10.html("<input type='text' name='f10' id='f10' value='"+f10.html()+"'/>");
            f11.html("<input type='text' name='f11' id='f11' value='"+f11.html()+"'/>");
            f12.html("<input type='text' name='f12'id='f12' value='"+f12.html()+"'/>");
            f13.html("<input type='text' name='f13' id='f13' value='"+f13.html()+"'/>");
            f14.html("<input type='text' name='f14' id='f14' value='"+f14.html()+"'/>");
            f15.html("<input type='text' name='f15' id='f15' value='"+f15.html()+"'/>");
            f16.html("<input type='text' name='f16' id='f16' value='"+f16.html()+"'/>");
            f17.html("<a onclick='edit_submit();' style='cursor: pointer;'>Save</a>");


            $(".btnSave").bind("click", Save);
            $(".btnEdit").bind("click", Edit);
            $(".btnDelete").bind("click", Delete);
        };

控制器

function edit()
{
    $R1 = $this->input->post('f1');
    $R2 = $this->input->post('f2');
    $R3 = $this->input->post('f3');
    $H1 = $this->input->post('valh1');
    $R4 = $this->input->post('f4');
    $R5 = $this->input->post('f5');
    $H2 = $this->input->post('valh2');
    $H3= $this->input->post('valh3');
    $R6 = $this->input->post('f6');
    $R7 = $this->input->post('f7');
    $R8 = $this->input->post('f8');
    $R9 = $this->input->post('f9');
    $R10 = $this->input->post('f10');
    $R11 = $this->input->post('f11');
    $R12 = $this->input->post('f12');
    $R13 = $this->input->post('f13');
    $H4 = $this->input->post('valh4');
    $R14 = $this->input->post('f14');
    $R15 = $this->input->post('f15');
    $R16 = $this->input->post('f16');

    $data = $R1.",".$R2.",".$R3.",".$H1.",".$R4.",".$R5.",".$H2.",".$H3.",".$R6.",".$R7.",".$R8.",".$R9.",".$R10.",".$R11.",".$R12.",".$R13.",".$H4.",".$R14.",".$R15.",".$R16;
    $list = array($data);

    $file = fopen("./bin/pdw_table.csv","a");

    foreach ($list as $line)
    {
        fgetcsv($file,explode(',',$line));
    }

    fclose($file);
    redirect('datacast_ctr');
}

【问题讨论】:

    标签: javascript php codeigniter csv


    【解决方案1】:

    你应该看看codeigniter中的文件库

    write_file('path', $data) 应该做你想做的。

    从这里得到它

    http://ellislab.com/codeigniter/user-guide/helpers/file_helper.html

    【讨论】:

    • 感谢您的帮助,但我已经使用并尝试过 PHP 中的文件系统函数。它可以帮助我添加使用aappend 的新数据,它将在End of file 添加新数据,但我的问题是编辑。我应该使用什么?
    最近更新 更多