Php 使用HTML表格删除CSV文件中的特定行

Php 使用HTML表格删除CSV文件中的特定行,php,html,csv,Php,Html,Csv,我有一个HTML表,我使用PHP。。表中的数据来自CSV文件 我已经可以在文件末尾的新行中添加新数据了。我的问题是,如果我想删除CSV文件中数据中间的行。我该怎么做?顺便说一句,我这里不使用任何数据库。我只使用HTML表和CSV文件。下面是我查看CSV和添加新数据的代码 查看 <?php $row = 1; if (($handle = fopen("bin/pdw_ta

我有一个
HTML表
,我使用
PHP
。。表中的数据来自
CSV文件

我已经可以在文件末尾的新行中添加新数据了。我的问题是,如果我想删除CSV文件中数据中间的行。我该怎么做?顺便说一句,我这里不使用任何数据库。我只使用
HTML表
CSV文件
。下面是我查看CSV和添加新数据的代码

查看

<?php
                            $row = 1;
                                if (($handle = fopen("bin/pdw_table.csv", "r+")) !== FALSE) {

                        ?>
                                    <table class="table table-hover table-striped table-bordered" id="table-data">
                                        <tr>
                                            <th>Field 1</th>
                                            <th>Field 2</th>
                                            <th>Field 3</th>
                                            <th></th>
                                            <th>Field 4</th>
                                            <th>Field 5</th>
                                            <th></th>
                                            <th></th>
                                            <th>Field 6</th>
                                            <th>Field 7</th>
                                            <th>Field 8</th>
                                            <th>Field 9</th>
                                            <th>Field 10</th>
                                            <th>Field 11</th>
                                            <th>Field 12</th>
                                            <th>Field 13</th>
                                            <th></th>
                                            <th>Field 14</th>
                                            <th>Field 15</th>
                                            <th>Field 16</th>
                                        </tr>
                        <?php
                                        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
                                            $num = count($data);
                        ?>
                                            <tr <?php if($row==0){echo "style='font-weight:bold; background-color:#CCCCCC'";} else {echo "style='background-color:#DDDDDD'";} ?> style="background-color:#DDDDDD">
                        <?php
                                                for ($c=0; $c < $num; $c++) {
                        ?>
                                                <td><?php echo $data[$c]; ?></td>  
                        <?php
                            }
                        ?>
                                            </tr>
                        <?php

                                        $row++;
                                        }
                                        fclose($handle);
                        ?>
                                        <form method="post" name="add1" id="add1" action="<?php echo base_url();?>index.php/datacast_ctr/write_csv" autocomplete="off">
                                            <tr class="td1" id="td1" >  
                                                <td><input type="text" name="val1" id="val1"/></td>
                                                <td><input type="text" name="val2" id="val2"/></td>
                                                <td><input type="text" name="val3" id="val3"/></td>
                                                <td></td>
                                                <td><input type="text" name="val4" id="val4"/></td>
                                                <td><input type="text" name="val5" id="val5"/></td>
                                                <td></td>
                                                <td></td>
                                                <td><input type="text" name="val6" id="val6"/></td>
                                                <td><input type="text" name="val7" id="val7"/></td>
                                                <td><input type="text" name="val8" id="val8"/></td>
                                                <td><input type="text" name="val9" id="val9"/></td>
                                                <td><input type="text" name="val10" id="val10"/></td>
                                                <td><input type="text" name="val11" id="val11"/></td>
                                                <td><input type="text" name="val12" id="val12"/></td>
                                                <td><input type="text" name="val13" id="val13"/></td>
                                                <td></td>
                                                <td><input type="text" name="val14" id="val14"/></td>
                                                <td><input type="text" name="val15" id="val15"/> </td>
                                                <td><input type="text" name="val16" id="val16"/></td>
                                            </tr>
                                        </form>
                                    </table>
                        <?php
                            }
                        ?>

好吧,你不能这么容易就删除一行。您必须打开文件,逐个读取所有行,并将它们放入输出文件中,但要删除或使用解决方案的行除外

如果您想知道如何实现这一点,这取决于csv文件中的数据。如果您有某种类型的唯一键,则可以使用按钮或任何调用传递此键的delete函数的内容添加另一列

编辑 这是伪代码,因为我现在没有php环境。但有点像 $filedata=“”; $line=“”

$f=fopen(“,”c+”);
而($line=fgets($f)){
如果($line!=“在此处检查该行是否为要删除的行”){
$data.=$line;
}
}
ftruncate($f,0);
FPUT($f$数据);
外国法郎(f美元);

感谢您的评论,但我已在您给我的链接上阅读了此答案,请逐行阅读,并将匹配行以外的所有内容写入另一个文件。然后替换原始文件。我的问题是,如何使用代码而不是手动替换文件?要用您刚刚使用的另一个文件替换文件。然而,我更愿意首先以读取模式打开文件,将所有内容读取到一个变量中,除了要删除的条目,然后将所有内容写入文件的开头。嗨,我已经尝试过你的代码,但它只是截断CSV文件中的数据。正如我所说,这是显示一般方法的伪代码。加载数据,清除文件并将修改后的数据放回。哦,我刚刚意识到fopen()调用需要一个读/写标志,否则不能在文件中放入任何内容。试试c+。我也编辑了答案。结果是
ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray
。代码将截断所有数据并替换
数组
function write_csv()
{
    $R1 = $this->input->post('val1');
    $R2 = $this->input->post('val2');
    $R3 = $this->input->post('val3');
    $H1 = $this->input->post('valh1');
    $R4 = $this->input->post('val4');
    $R5 = $this->input->post('val5');
    $H2 = $this->input->post('valh2');
    $H3= $this->input->post('valh3');
    $R6 = $this->input->post('val6');
    $R7 = $this->input->post('val7');
    $R8 = $this->input->post('val8');
    $R9 = $this->input->post('val9');
    $R10 = $this->input->post('val10');
    $R11 = $this->input->post('val11');
    $R12 = $this->input->post('val12');
    $R13 = $this->input->post('val13');
    $H4 = $this->input->post('valh4');
    $R14 = $this->input->post('val14');
    $R15 = $this->input->post('val15');
    $R16 = $this->input->post('val16');
    $H5 = $this->input->post('valh5');

    $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)
    {
        fputcsv($file,explode(',',$line));
    }

    fclose($file);
    redirect('datacast_ctr');
}
$f = fopen("<filename>", "c+");
while($line = fgets ($f)){
    if($line != "check here if the line is the one to delete"){
        $data.= $line;
    }
}
ftruncate($f,0);
fputs($f, $data);
fclose($f);