Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by duncanb7
at 2024-04-25 00:27:11
Point:500 Replies:9 POST_ID:828482USER_ID:11059
Topic:
PHP Scripting Language;;JavaScript
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
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
<?phpecho "Hello Together:";$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);?> 1:2:3:4:5:6:7:8:9:10:11:12:13:14:
Author: duncanb7 replied at 2024-04-25 05:56:03
Thanks for your reply
It is good start
It is good start
Author: duncanb7 replied at 2024-04-25 05:54:53
$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
Expert: nepaluz replied at 2024-04-25 05:18:17
It tells the next line by array_shift() while iterating through the loaded file contents in the array.
Author: duncanb7 replied at 2024-04-25 05:06:08
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 ?
Accepted Solution
Expert: nepaluz replied at 2024-04-25 04:46:35
500 points EXCELLENT
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); 1:2:3:4:5:6:7:8:9:10:11:12:13:
Expert: nepaluz replied at 2024-04-25 04:43:10
I missed out an important line in the above code. Insert this between line 8 and 9 above:
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); 1:2:3:4:5:6:7:8:9:10:11:12:
Expert: nepaluz replied at 2024-04-25 04:40:02
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); 1:2:3:4:5:6:7:8:9:10:11:
Author: duncanb7 replied at 2024-04-25 01:28:03
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
Expert: khan_webguru replied at 2024-04-25 01:12:05
You can use fwrite its part of php5.0
Please see this link
http://php.net/manual/en/function.fwrite.php
Please see this link
http://php.net/manual/en/function.fwrite.php