1

I have a HTML table and i usePHP.. the data inside the table is came from an CSV file.

I can already add a new data in a new row at the end of file. My problem is, what if i want to delete existing row that is in the middle of the data in the CSV file. How can i do that? By the way i don't use any database here. I just use HTML Table and CSV file. Below is my code in viewing CSV and adding new data.

View

<?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
                            }
                        ?>

CONTROLLER (code for adding new data EOF)

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');
}
bebebe
  • 339
  • 2
  • 7
  • 20

1 Answers1

2

AFAIK you can't just delete one line that easy. You have to either open the file, read all lines one after another and put them into the output file except the one you want to delete or use this solution.

If you want to know how to implement this, it depends on the data in your csv file. If you have some sort of unique key you can add another column with a button or anything that calls the delete function passing this key.

EDIT This is pseudo code as i don't have a php environment here at the moment. But something like $filedata = ""; $line = "";

$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);
Community
  • 1
  • 1
Bowdzone
  • 3,827
  • 11
  • 39
  • 52
  • Thank you for your comment but i have read this answer on the link that you have given to me **Read the lines one by one, and write all but the matching line to another file. Then replace the original file.** My question is how can i replace the file using code and not manually? – bebebe Mar 24 '14 at 09:28
  • 1
    To replace a file with another file you just use [rename](http://www.php.net/manual/en/function.rename.php). I would however prefer to first open the file in read mode, read all contents to a variable except the entry you want to delete and the write everything to the beginning of the file. – Bowdzone Mar 24 '14 at 10:00
  • hi i have already tried your code but it just truncate the data in CSV file. – bebebe Mar 26 '14 at 04:04
  • As I said, this is pseudo code to show the general approach. Load the data, clear the file and put the modified data back in. Oh I just realized the fopen() call needs a flag for read/write, otherwise you can't put anything in the file. Try "c+" instead. I edited the answer as well. – Bowdzone Mar 26 '14 at 08:23
  • the output is this `ArrayArrayArrayArrayArrayArrayArrayArrayArrayArray`. The code will truncate all the data and replace a `Array` – bebebe Mar 26 '14 at 08:56
  • This may have different reasons. I can't tell why this is happening without looking at the code. – Bowdzone Mar 26 '14 at 10:00