Jump to content

RESOLVED-form, php script and csv file - and a darn comma is causing a problem


kenwvs

Recommended Posts

I have an html form that once it has been completed, a php script takes over and saves it to a csv file and lets you save it as well as send it back to you by email.  In one of the fields you fill in, it would be very common to use a comma (,) which causes an additional field to be created.  Normally, if you use quotation marks (") at the start and end of this field, it will ignore the comma and keep all the text as one field.  I am not able to figure out how to get it to do this in either to form or the script.

The field that causes the grief is item_description.  In my novice mind, I thought adding the quotation marks in the script on the outside of the ' might work, but it didn't and then tried it on the inside of the ' and that didn't work either.  Just had a thought and am wondering if you can put them inside the input field of the form and have them hidden, so the users can't take them out.  Not sure, but here is the script and partial form.

The php script being used is:

[code]<?php
$flds = array('item_title','item_category','item_type','quantity_available','starting_bid/price','bid_increment','reserve_price','duration','auto_relist','city','state','country',"'item_description'",'paypal_id','hit_counter','end_hour');
$write_to_csv = 'UploadForm/' . $_POST['id'] . '.csv';
if (!file_exists($write_to_csv)) { // only write the header if the file doesn't already exist
   $fp = fopen($write_to_csv,'a');
   fwrite($fp,implode(',',$flds)."\n"); // write the field names separated by commas
   fclose($fp); }
$fp = fopen($write_to_csv,'a');
$tmp = array();
foreach($flds as $fld)
    $tmp[] = $_POST[$fld]; // put the values in the temp array in the correct order.
fwrite($fp,implode(',',$tmp)."\n"); // write to the file
fclose($fp);
header("Location: http://www.forsale4u.ca/uploadformconfirmation.html");
?>[/code]

As the html form is quite long, I am including only the portion around the field in question.

[quote]<p><font face="Arial"><font size="2">Item Description </font> <font size="3">
&nbsp;<textarea rows="4" name="item_description" cols="108"></textarea></font></font></p> [/quote] 
by doing it this way, would it still sort the file into the order that I need it in.  Is the line you added going right after the line with the first $tmp[].  I am not sure I am understanding what  is going to happen here.

Ken
if $flds is the array of values you want to write to the file, then this will put ".." round each value in the csv

fwrite($fp, '"'. implode('","',$flds)."\"\n"); // write to the file

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.