websoftexpert Posted February 23, 2013 Share Posted February 23, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274856-editing-csv-file-in-php/ Share on other sites More sharing options...
Barand Posted February 23, 2013 Share Posted February 23, 2013 Have you tried fgetcsv and fputcsv Quote Link to comment https://forums.phpfreaks.com/topic/274856-editing-csv-file-in-php/#findComment-1414400 Share on other sites More sharing options...
websoftexpert Posted February 23, 2013 Author Share Posted February 23, 2013 (edited) 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 February 23, 2013 by websoftexpert Quote Link to comment https://forums.phpfreaks.com/topic/274856-editing-csv-file-in-php/#findComment-1414402 Share on other sites More sharing options...
websoftexpert Posted February 23, 2013 Author Share Posted February 23, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/274856-editing-csv-file-in-php/#findComment-1414406 Share on other sites More sharing options...
Barand Posted February 23, 2013 Share Posted February 23, 2013 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); Quote Link to comment https://forums.phpfreaks.com/topic/274856-editing-csv-file-in-php/#findComment-1414454 Share on other sites More sharing options...
websoftexpert Posted February 23, 2013 Author Share Posted February 23, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274856-editing-csv-file-in-php/#findComment-1414468 Share on other sites More sharing options...
Barand Posted February 23, 2013 Share Posted February 23, 2013 (edited) 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 February 23, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274856-editing-csv-file-in-php/#findComment-1414469 Share on other sites More sharing options...
websoftexpert Posted February 24, 2013 Author Share Posted February 24, 2013 Thanks, its done. Quote Link to comment https://forums.phpfreaks.com/topic/274856-editing-csv-file-in-php/#findComment-1414658 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.