Replace the existing data in CSV file using php or CodeIgniter -


if a in php write @ end of file , w write @ beginning of file

what if want edit existing data in csv file , location of data in middle row. should use update existing data or edit it?

below code in controller , view.. need know the edit , delete automatically update csv file. thank in advance :)

controller

<?php if ( ! defined('basepath')) exit('no direct script access allowed');  class datacast_ctr extends ci_controller {      function __construct()     {         parent::__construct();         $this->load->library('csvreader');         $this->load->helper('csv');     }      public function index()     {        $this->load->library('csvreader');         $filepath = 'c:\xampp\htdocs\datacast\bin\pdw_table.csv';         $data['csvdata'] = $this->csvreader->parse_file($filepath);         $this->load->view('datacast_view', $data);     }      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');          $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');     } } 

view

<head>      <title> datacast </title>     <!-- css stylesheets -->     <link href="<?php echo base_url();?>source/css/bootstrap.css" rel="stylesheet" media="screen">       <link href="<?php echo base_url();?>source/css/style.css" rel="stylesheet" media="screen">       <!-- jquery -->     <script src="http://code.jquery.com/jquery.js"></script>     <script src="<?php echo base_url();?>source/js/bootstrap.min.js"></script>     <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />     <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>     <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>     <script type="text/javascript" src="jquery-1.7.2.min.js"></script>     <script type="text/javascript" src="functions.js"></script>     <link href="<?php echo base_url(); ?>plugins/jquery_ui/jquery-ui.css" rel="stylesheet" type="text/css">     <script type="text/javascript" src="<?php echo base_url(); ?>plugins/jquery_ui/jquery-1.9.1.js"></script>     <script type="text/javascript" src="<?php echo base_url(); ?>plugins/jquery_ui/jquery-ui.js"></script>      <script>          $(function() {                 $('#btnadd').click(function() {                      $('.td1').toggle('show');             });          });           function submit()          {              document.getelementbyid('add1').submit();              return true;          }           function edit_submit()          {              document.getelementbyid('edit1').submit();              return true;          }           $(document).ready(function(){              $('.btnedit').editable();          });      </script>  </head>  <body> <br/><br/><br/>     <div class="row-fluid offset9">                 <div class="container">                    <div class="row">                        <div id="navigation-menus">                                         <div id="btnadd" name="btnadd" class="span1" style="cursor: pointer;">                                    <div class="menu-add">add</div>                            </div>                                       </div>                                       </div>                </div>     </div>             <div id="div1" class="row-fluid">                     <div class="span12">                             <table class="table table-hover table-striped table-bordered" id="table-data">                                     <thead>                     <tr>                         <th>field 1</th>                         <th>field 2</th>                         <th>field 3</th>                         <th>field 4</th>                         <th>field 5</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>field 14</th>                         <th>field 15</th>                         <th>field 16</th>                     </tr>                 </thead>                 <tbody>                                         <?php foreach($csvdata $field)                                             {                                         ?>                     <tr>                         <td><?php echo $field['field 1']; ?></td>                         <td><?php echo $field['field 2']; ?></td>                         <td><?php echo $field['field 3']; ?></td>                         <td><?php echo $field['field 4']; ?></td>                         <td><?php echo $field['field 5']; ?></td>                         <td><?php echo $field['field 6']; ?></td>                         <td><?php echo $field['field 7']; ?></td>                         <td><?php echo $field['field 8']; ?></td>                         <td><?php echo $field['field 9']; ?></td>                         <td><?php echo $field['field 10']; ?></td>                         <td><?php echo $field['field 11']; ?></td>                         <td><?php echo $field['field 12']; ?></td>                         <td><?php echo $field['field 13']; ?></td>                         <td><?php echo $field['field 14']; ?></td>                         <td><?php echo $field['field 15']; ?></td>                           <td><?php echo $field['field 16']; ?></td>                                                     <td><a class="btnedit" onclick="edit();" style="cursor: pointer;">edit</a> <a style="cursor: pointer;" class="btndelete">delete</a></td>                     </tr>                                         <?php }?>                                       <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 style="display: none;"><input type="hidden" name="valh1" id="valh1"/></td>                                                 <td><input type="text" name="val4" id="val4"/></td>                                                 <td><input type="text" name="val5" id="val5"/></td>                                                 <td style="display: none;"><input type="hidden" name="valh2" id="valh2"/></td>                                                 <td style="display: none;"><input type="hidden" name="valh3" id="valh3"/></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 style="display: none;"><input type="hidden" name="valh4" id="valh4"/></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>                                                 <td><a onclick="submit();" style="cursor: pointer;">save</a></td>                                             </tr>                                          </form>                 </tbody>             </table>         </div>       </div>      <div class="row-fluid">         <div class="span12"></div>     </div>   </body> 

this simple example using file_get_contents , file_put_contents. should not used big csv files , used describe possible method:

$csvdata = file_get_contents($filename); $csv = explode("\n", $csvdata); $newdata = array(); foreach($csv $rowdata) {     $row = explode(";", $rowdata);     //change data here     $newdata[] = implode(";", $row); } $newdata = implode("\n", $newdata); file_put_contents($filename, $newdata); 

if want parse big csv files can have @ examples provided here


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -