• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1072
  • Last Modified:

update one locate cell value in same csv file by php's fopen (".csv", "r+")

Dear Expert,

I would like to open a csv file by a simple php  code and read data and update/write  data in a certain row and colum
in the same csv file, and put one condition, for example, if everyline's first column cell is >0 , then update the cell to 123222
But it is fail to fputcsv and the csv file is nothing change, just can be read by echo, Why ?
 please help to view my code. If possible, provide similar code for a start.

OR is it the problem the fputcsv function doesn't include fwrite function ? I am using PHP 5.0.0

and I am using fopen with  "r+" option for read and writing and put the point at the beginning of the file
that should be okay, Right ?

Please advise

Duncan


<?php
echo "Hello  Together: \n";
$file_handle = fopen("data.csv", "r+");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
if ($line_of_text[0] >0 ) {
echo $line_of_text[0].'<br></br>' ;
$line_of_text[0] =1232222;
  fputcsv($file_handle, $line_of_text);
    }
}
fclose($file_handle);

?>

Open in new window

0
duncanb7
Asked:
duncanb7
  • 4
  • 4
1 Solution
 
khan_webguruCommented:
You can use fwrite its part of php5.0

Please see this link

http://php.net/manual/en/function.fwrite.php
0
 
duncanb7Author Commented:
it is probably  relate to rewind or fseek(), that I need
to offset file pointer at the same location that is being read before for
writing the same location of every line's first column cell


For example, csv file
12,13,16,12
13,15,17,10
20,222,2212,223
that should be changing to as follows
1232222,13,16,12
1232222,15,17,10
1232222,222,2212,223


I try to us
fseek($file_hanlde, -1024, SEEK_CUR)
but it doesn't work and give me warning "Warning: fseek() [function.fseek]: stream does not support seeking in lin ..."
I check my version of PHP is 5.2.4 that should be okay , Is it right.?

The site you suggested I read a lot of time, thanks, please adivse
 
0
 
nepaluzCommented:
I have handled a similar situation by reading the file into an array, iterating through the array and rewritting the file from the amended array.Here is the code that should do it for you.

$file = "data.csv";
$proverbs = file($file);
$num = count($proverbs);
for ($c=0; $c < $num; ++$c) {
	$line_of_text = explode(',', $custom);
	if ($line_of_text[0] > 0) {
                   echo $line_of_text[0].'<br></br>' ;
                   $line_of_text[0] =1232222;
	}
}
file_put_contents($file, $proverbs);

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nepaluzCommented:
I missed out an important line in the above code. Insert this between line 8 and 9 above:
	$proverbs[] = implode(",",$line_of_text);

Open in new window

so you have
$file = "data.csv";
$proverbs = file($file);
$num = count($proverbs);
for ($c=0; $c < $num; ++$c) {
	$line_of_text = explode(',', $custom);
	if ($line_of_text[0] > 0) {
                   echo $line_of_text[0].'<br></br>' ;
                   $line_of_text[0] =1232222;
	   $proverbs[] = implode(",",$line_of_text);
	}
}
file_put_contents($file, $proverbs);

Open in new window


0
 
nepaluzCommented:
this is becoming embarrasing, I left out ANOTHER line where you shift the array line per line, here is the complete code now.
$file = "data.csv";
$proverbs = file($file);
$num = count($proverbs);
for ($c=0; $c < $num; ++$c) {
	$custom =  array_shift($proverbs);
	$line_of_text = explode(',', $custom);
	if ($line_of_text[0] > 0) {
                   echo $line_of_text[0].'<br></br>' ;
                   $line_of_text[0] =1232222;
	   $proverbs[] = implode(",",$line_of_text);
	}
}
file_put_contents($file, $proverbs);

Open in new window



0
 
duncanb7Author Commented:
It works fine, and
THe final question,

What is explode and implode ?
How it is tell the next line is ?
0
 
nepaluzCommented:
It tells the next line by array_shift() while iterating through the loaded file contents in the array.
0
 
duncanb7Author Commented:
$num = count($proverbs);
that will tell the number of line.

And if next line 's first cell data is &#20195 as Chinese code , the $num or count() function
could NOT recongize it is next line so it will report less number of line in the file


Duncan
0
 
duncanb7Author Commented:
Thanks for your reply
It is good start
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now