Re: $_POST and CSV File
- From: Michel Beaussart <michel@xxxxxxxxxxxxxx>
- Date: Fri, 28 Oct 2005 13:24:02 -0700
steve wrote:
| //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 *** 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
Steve,
Thank you very much for your input.
Your suggestion for the last column has been implemented.
As for my csv file the problem is still existing as I do not know where to begin.
I have a specific header in the csv file. Each columns name corresponds to a form element's name.
So that:
<input type='text' name='rded1' size='40' maxlength='30'>
has a column named rded1
BUT how do I specify that (Pcode):
foreach $_POST($column) as $k=>$v
since the $_POST is not necessarely coming in the columns order from the CSV file? (making it impossible to put the right $v in the right column).
Once again .. thank you for your tip!
.
- Follow-Ups:
- Re: $_POST and CSV File
- From: steve
- Re: $_POST and CSV File
- References:
- $_POST and CSV File
- From: Michel Beaussart
- Re: $_POST and CSV File
- From: steve
- $_POST and CSV File
- Prev by Date: Re: exec
- Next by Date: Re: $_POST and CSV File
- Previous by thread: Re: $_POST and CSV File
- Next by thread: Re: $_POST and CSV File
- Index(es):