Jump to content


Photo

Editing CSV File in PHP


  • Please log in to reply
7 replies to this topic

#1 websoftexpert

websoftexpert

    Member

  • Members
  • PipPip
  • 21 posts

Posted 23 February 2013 - 06:03 AM

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

Attached Files



#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,184 posts
  • LocationCheshire, UK

Posted 23 February 2013 - 06:15 AM

Have you tried fgetcsv and fputcsv

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 websoftexpert

websoftexpert

    Member

  • Members
  • PipPip
  • 21 posts

Posted 23 February 2013 - 06:39 AM

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, 23 February 2013 - 06:42 AM.


#4 websoftexpert

websoftexpert

    Member

  • Members
  • PipPip
  • 21 posts

Posted 23 February 2013 - 07:24 AM

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);


#5 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,184 posts
  • LocationCheshire, UK

Posted 23 February 2013 - 01:10 PM

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);

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#6 websoftexpert

websoftexpert

    Member

  • Members
  • PipPip
  • 21 posts

Posted 23 February 2013 - 02:47 PM

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

#7 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 14,184 posts
  • LocationCheshire, UK

Posted 23 February 2013 - 03:20 PM

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, 23 February 2013 - 03:35 PM.

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#8 websoftexpert

websoftexpert

    Member

  • Members
  • PipPip
  • 21 posts

Posted 24 February 2013 - 09:53 AM

Thanks, its done.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com