Jump to content


Photo

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


  • Please log in to reply
4 replies to this topic

#1 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 11 July 2006 - 07:25 PM

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:

<?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");
?>

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

<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>

 

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 11 July 2006 - 07:39 PM

<?php
while ($flds = mysql_fetch_row($result)) {
 
         fwrite($fp, '"'. implode('","',$flds)."\"\n"); // write to the file
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 11 July 2006 - 08:57 PM

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

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 11 July 2006 - 09:01 PM

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
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 11 July 2006 - 09:39 PM

That solved my Problem.  Thank You very much




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users