Jump to content

Recommended Posts

Hi

 

I want to edit value of csv file and save edited version of csv file based on condition.

 

I know how to put condition but do not how to edit that particular line and position which I want to edit in php

 

attached is sample csv file in pdf format in which I want to edit.

 

There are column Unit1, Unit2, Unit3, Unit4 etc

 

suppose if value of unit4 of day 8 is below 100 then I can reset it value to 1000 by php code and save csv file

 

 

Thanks for heping us

sample_csv.pdf

Link to comment
https://forums.phpfreaks.com/topic/274856-editing-csv-file-in-php/
Share on other sites

Here is code I written

 



$myFile = "sample_csv.csv";
//exit();
/**/

$fh = fopen($myFile, 'r+');
$theData = fread($fh, filesize($myFile));
//$theData = fgets($fh);
$theData = explode("\n", $theData);
//print_r($theData);
//$theData = nl2br($theData); // converting line break in "<br />"
for($i=1; $i<(count($theData)-1);$i++)
{
 $theDataArray = explode(",",$theData[$i]);
 print "<P>";
 //print_r($theDataArray);
 print "</P>";
 //exit;
 $unit1 = trim($theDataArray[4]);
 $unit2 = trim($theDataArray[5]);
 $unit3 = trim($theDataArray[6]);
 $unit4 = trim($theDataArray[7]);
 $unit5 = trim($theDataArray[8]);


 print "<P>";
 foreach ($theDataArray as $fields)
 {
 print $fields;
 print ",";
 if(($fields < 1000) && $fields)
 {
 $fields = 10000;
 }
 fputcsv($fh, $fields);
 }

 print "</P>";

}// for($i=1; $i<(count($theData)-1);$i++)

fclose($fh);

 

Please check and let me know what is wrong in above code. The above is reading csv file but not editing.

Edited by websoftexpert

Another code but not working

 




  $myFile = "sample_csv.csv";
  //exit();
  /**/

   $fh = fopen($myFile, 'r+');

   while (($data = fgetcsv($fh, 1000, ",")) !== FALSE) 
   {
  $num = count($data);
  echo "<p> $num fields in line $row: <br /></p>\n";
  $row++;
  for ($c=0; $c < $num; $c++) 
  {
   // echo $data[$c] . "<br />\n";
    if($c>3)
    {
    //echo $c . "<br />\n";
    echo $data[$c] . "<br />\n";
	  $fields = $data[$c];

    if(($fields < 1000) && $fields)
    {
	 $fields = 10000;
    }
    fputcsv($fh, $fields);
    }
  }
   }


fclose($fh);

I prefer creating a new file when changing text files

 

When you use foreach it copies the array items so you update the copy

 

<?php
$myfile = 'sample_csv.csv';
$newfile = 'sample2_csv.csv';

$fin = fopen($myfile, 'r');
$fout = fopen($newfile, 'w');

   /***********
   * header row
   */
$heads = fgetcsv($fin, 1000);
fputcsv($fout, $heads);
  /***********
   * data rows
   */
while ($line = fgetcsv($fin, 1000)) {
   echo join(', ', $line).'<br>';
   for($i = 4, $k = count($line); $i < $k; $i++) {
    if ($line[$i] < 1000) {
	    $line[$i] = 10000;
    }
   }
   fputcsv($fout, $line);
}

fclose($fin);
fclose($fout);

Thanks for valuable reply. But I need to edit existing csv file as it used by other php files for displaying data on web application. Also csv file is created automatically by another software on regular interval based on stock available in real time. In real time stock may drop below prescribed limit. So we need to handle this by editing existing csv file.

 

Thanks

In that case,

open file in read mode.

As you change each line store it in an array.

Close file and re-open in write mode.

Loop through array and write the lines

Close file

 

$myfile = 'sample_csv.csv';

$fin = fopen($myfile, 'r');
$data = array();

   /***********
   * header row
   */
$data[] = fgetcsv($fin, 1000);
  /***********
   * data rows
   */
while ($line = fgetcsv($fin, 1000)) {
   echo join(', ', $line).'<br>';
   for($i = 4, $k = count($line); $i < $k; $i++) {
    if ($line[$i] < 1000) {
	    $line[$i] = 10000;
    }
   }
   $data[] = $line;
}

fclose($fin);
   /******************
   * reopen file and
   * write array to file
   */
$fout = fopen($myfile, 'w');
foreach ($data as $line) {
   fputcsv($fout, $line);
}
fclose($fout);

Edited by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.