更新表格html并保存到csv

时间:2014-03-07 09:20:45

标签: javascript php codeigniter csv

我是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);
        };

CONTROLLER

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');
}

1 个答案:

答案 0 :(得分:0)

您应该查看codeigniter中的文件库

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

从这里得到它

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