duncanb7
asked on
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
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);
?>
ASKER
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
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
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);
I missed out an important line in the above code. Insert this between line 8 and 9 above:
$proverbs[] = implode(",",$line_of_text);
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);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It works fine, and
THe final question,
What is explode and implode ?
How it is tell the next line is ?
THe final question,
What is explode and implode ?
How it is tell the next line is ?
It tells the next line by array_shift() while iterating through the loaded file contents in the array.
ASKER
$num = count($proverbs);
that will tell the number of line.
And if next line 's first cell data is 代 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
that will tell the number of line.
And if next line 's first cell data is 代 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
ASKER
Thanks for your reply
It is good start
It is good start
Please see this link
http://php.net/manual/en/function.fwrite.php