php - Delete specific row in CSV file using HTML Table -


i have html table , usephp.. data inside table came csv file.

i can add new data in new row @ end of file. problem is, if want delete existing row in middle of data in csv file. how can that? way don't use database here. use html table , csv file. below code in viewing csv , 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                                                 ($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 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 $line)     {         fputcsv($file,explode(',',$line));     }      fclose($file);     redirect('datacast_ctr'); } 

afaik can't delete 1 line easy. have either open file, read lines 1 after , put them output file except 1 want delete or use this solution.

if want know how implement this, depends on data in csv file. if have sort of unique key can add column button or calls delete function passing key.

edit pseudo code don't have php environment here @ moment. $filedata = ""; $line = "";

$f = fopen("<filename>", "c+"); while($line = fgets ($f)){     if($line != "check here if line 1 delete"){         $data.= $line;     } } ftruncate($f,0); fputs($f, $data); fclose($f); 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -