|
Posted by steve on 10/13/41 11:30
| //Open csv file and grab first row
| $fp = fopen ("data/data.csv",'a+'); // open file
| $data = fgetcsv($fp, 1000, ","); // get csv
|
| // write data in a csv file
|
|
| foreach ($_POST as $data => $v) //data is over written ..:(
| {
|
| fwrite ($fp, "$v,");
| ;
| }
your "$_post as $data" makes your first assignment of $data ("$data =
fgetcsv(...)") irrelavent. since your foreach doesn't apparently use the key
("$data") then just use:
foreach ($_post as $v){ /* your code here */ }
if you *are* using the key then try:
foreach ($_post as $column => $value)){ /* your code here */ }
the reason for the problem is that you're assigning different values to the
same variable. what i don't understand - and perhaps it's just the snippet
example - is why you are using fgetcsv at all since you are opening the csv
file for append ("a+")...all that's needed in that case is for you to write
to the file.
one other thing...your snippet is going to fuck up your csv output anyway
since you blindly write "$v,"...that means the last real column/value pair
will end with "," . when you go to process that file, it will seem to have
an additional column whos value is blank. you'd have to code around that
everytime you wanted to process it...this is the place to fix it. i'd
recommend:
$fp = fopen('data/data.csv', 'a+');
fwrite('"' . implode('", "', $_POST) . '"');
fclose($fp);
that, of course, is just the simplification of your snippet which may not
reflect everything you're trying to do specifically in your production code.
next thing you should think about is not blindly assuming that the post
variables always come in the same order...lots of simple things can effect
that! the fix is very easy..but i'll leave you to that.
hth,
me
Navigation:
[Reply to this message]
|