kenwvs Posted July 6, 2006 Share Posted July 6, 2006 Good DayI have generated a form and a script (see below) that will automatically generate a csv file when the the form is submitted. The program I am using this with requires that the subject titles (headings) be the first line in the csv file. I am thinking I could manipulate the form to make them appear, but I am thinking there must be a way to have it happen in the php script. I have played around trying to get it to work with headers and by trying to just write them into the script, both with no success, just errors.Anyone care to give me a hand in getting this to happen.This script may not be appealing to the eye, or be programmer correct, but it works, and for that reason I am happy as this is the my first attempt at programming. Right now it will create a php file, name it whatever is chosen on the form, fill in the blanks and save it to a subdirectory on the server. I am still trying to figure out how to email this in a csv format to the person who filled out the form OR how to have the file pop up on the screen and give the person an opportunity to save it to their computer.Thanks for the help and here is the script. Ken<?php//this line will create the file name based on the form entry$writetocsv = $_POST['id'];$write_to_csv = "UploadForm/$writetocsv".".csv";//these lines will enter the form fields into the file, in the required order$writetocsv = $_POST['item_title'] . "," . $_POST['item_category'] . "," . $_POST['item_type'] . "," . $_POST['quantity_available'] . "," . $_POST['starting_bid'] . "," . $_POST['bid_increment'] . "," . $_POST['reserve_price'] . "," . $_POST['duration'] . "," . $_POST['auto_relist'] . "," . $_POST['city'] . "," . $_POST['state'] . "," . $_POST['country'] . "," . $_POST['item_description'] . "," . $_POST['paypal_id'] . "," . $_POST['hit_counter'] . "," . $_POST['end_hour'] . "\n";//these lines will write the file and save it to the file created above$fh = fopen("$write_to_csv", 'a') or die("can't create file");fwrite($fh,"$writetocsv");//this line will send the user to a confirmation pageheader("Location: http://www.forsale4u.ca/uploadformconfirmation.html");//this line will close the filefclose($fh);?> Quote Link to comment https://forums.phpfreaks.com/topic/13857-getting-the-titles-in-a-csv-file-that-is-automatically-generated-in-php/ Share on other sites More sharing options...
kenrbnsn Posted July 6, 2006 Share Posted July 6, 2006 The best way to do this is to set up an array of the field names that would be used to write the headers and the fields:[code]<?php$flds = array('item_title','item_category','item_type','quantity_available','starting_bid','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,'w'); 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 filefclose($fp);?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/13857-getting-the-titles-in-a-csv-file-that-is-automatically-generated-in-php/#findComment-53952 Share on other sites More sharing options...
Buyocat Posted July 6, 2006 Share Posted July 6, 2006 What do you mean "subject titles"? If you mean something that isn't found in the form data, and something that is consistent thoughout each file, then why not create a new variable $_header = 'whatever'; // stick this at the beginning of the file writeAs for emailing and new windowing, you can email using the mail() function, I suggest looking it up at php.net, it's pretty simple. New windows can be opened with javascript or you can try this html...<a href='the_new_file.php' target='newwindow'>Open the new file</a>Something like that, bear in mind it will open the file and process it, ie not for editing. Quote Link to comment https://forums.phpfreaks.com/topic/13857-getting-the-titles-in-a-csv-file-that-is-automatically-generated-in-php/#findComment-53953 Share on other sites More sharing options...
kenwvs Posted July 6, 2006 Author Share Posted July 6, 2006 I have used the code suggested, and found listed below and it works, but am encountering one problem when I go to import it into the actual program it has been designed for. On the line where it puts the items in the correct order, if I change it from the default answer on the form, it moves the new answer (which is a time in a number format 1, 2, 3 etc.) over one column and puts a 0 in the proper box. If I don't change the default answer, it works properly. Any ideas what is causing this and is it possible to somehow watch the routine run in slow motion and see what is actually happening? I know that sounds dumb, but I would like to be able to watch it do everything, just to get a feel for it.Thanks,Ken<?phpheader("Location: http://www.forsale4u.ca/uploadformconfirmation.html");$flds = array('item_title','item_category','item_type','quantity_available','starting_bid','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 filefclose($fp);?> Quote Link to comment https://forums.phpfreaks.com/topic/13857-getting-the-titles-in-a-csv-file-that-is-automatically-generated-in-php/#findComment-54032 Share on other sites More sharing options...
kenrbnsn Posted July 6, 2006 Share Posted July 6, 2006 Can you post the source for the form? Or the script that generates the form?Also, why do you have the header() function before the rest of the script? It really should go at the end of the script.Ken Quote Link to comment https://forums.phpfreaks.com/topic/13857-getting-the-titles-in-a-csv-file-that-is-automatically-generated-in-php/#findComment-54060 Share on other sites More sharing options...
kenwvs Posted July 6, 2006 Author Share Posted July 6, 2006 KenI am including below the code for the form. I have the header at the top, only because I read somewhere that it had to come before something else ran, and in my beginner thinking, thought this meant it had to go at the top. I will move it to the end. Does it make a difference in the way it works, depending on its location?Thanks again,Ken<html></body></html><head><meta http-equiv="Content-Language" content="en-us"><meta http-equiv="Content-Type" content="text/html; charset=windows-1252"><title>For Sale 4 U</title></head><body bgcolor="#F5F3F5"><p align="center"><br><b><font size="4" face="Arial">For Sale 4 U</font></b></p><p align="center"><b><font size="4" face="Arial">Multiply Item Upload Form</font></b></p><p align="left"><font face="Arial" size="2"><b>Please note: </b>You may not use characters such as $, #, ", &, *, etc. It is best to use normal text and numerical characters. If you use a character that is not permitted, the item will not be successfully uploaded.</font></p><p align="left"><font face="Arial" size="2">If you have more than 15 items to upload you may complete this form, submit it, and then repeat the process.</font></p><p align="left"><font face="Arial" size="2">If you have difficulty in successfully uploading your file, once you have received it by email, please contact us by<a href="mailto:upload@forsale4u.ca?subject=Multiple Upload Troubleshooting">EMAIL</a> and we will gladly assist you in getting it uploaded. Please be sure to include the file you have received as an attachment.</font></p><form action="upload2.php" method="POST"> <p><b><font face="Arial" size="4">ITEM #1</font></b></p> <p><font face="Arial"><font size="2">Item Title </font> <font size="3"> <input type="text" name="item_title" size="73"></font><font size="2"> Category: </font><select size="1" name="item_category"> <option value="000">Please Select Category</option> <option value="150">Antiques</option> <option value="18">Automobiles - Cars</option> <option value="75">Home and Garden - Lawnmowers</option> <option value="76">Home and Garden - Rototillers</option> <option value="77">Home and Garden - Gardening Tools and small equipment </option> <option value="78">Home and Garden - Services</option> <option value="79">Home and Garden - Sheds</option> <option value="92">Home Electronics - Gaming Machines</option> <option value="87">Computers and Accessories - Monitors</option> <option value="88">Computers and Accessories - Printers</option> <option value="89">Computers and Accessories - CPU's</option> <option value="90">Computers and Accessories - Complete Systems</option> <option value="91">Computers and Accessories - Accessories</option> <option value="103">Computers and Accessories - Services</option> <option value="98">Televisions - Flat Screen</option> <option value="99">Televisions - Under 20"</option> <option value="100">20 inch and over</option> <option value="101">Televisions - Plasma</option> <option value="84">Cameras - Digital Camera</option> <option value="85">Cameras - Camcorders</option> <option value="86">Cameras - Others</option> <option value="93">Stereos - Speakers</option> <option value="94">Stereos - Components</option> <option value="95">Stereos - All in One</option> <option value="96">Home Electronics - DVD and VCR</option> <option value="97">Home Electronics - MP3, Discman, etc.</option> <option value="105">Construction - Services</option> <option value="106">Construction - Equipment</option> <option value="107">Construction - Tools</option> <option value="109">Construction - Products - New</option> <option value="110">Construction - Products - Used</option> <option value="118">Employment Ads - Retail</option> <option value="119">Employment Ads - Restaurant</option> <option value="120">Employment Ads - General Labourer</option> <option value="121">Pets</option> <option value="122">Services - Professional</option> <option value="123">Services - Tradesman</option> <option value="124">Services - Child Care</option> <option value="126">Services - Yard Care and Snow Removal</option> <option value="127">Services - Moving and Storage</option> <option value="128">WANT ADS</option> <option value="134">Automotive Stereos and Radar Detectors</option> <option value="135">Home Electronics - Telephone</option> <option value="136">Home Electronics</option> <option value="137">Personal Care Products</option> <option value="138">Toys</option> <option value="113">Arts and Crafts</option> <option value="130">Jewellery and Watches - Watches</option> <option value="131">Jewellery and Watches - Rings</option> <option value="132">Jewellery and Watches - Beaded Jewellery</option> <option value="133">Jewellery and Watches - Engagement and Wedding</option> <option value="102">Televisions - Projection</option> <option value="19">Automobiles - Trucks</option> <option value="20">Automobiles - Antique Vehicles</option> <option value="21">Automobiles - Hot Rod Vehicles</option> <option value="27">Recreational Vehicle - 5th Wheels</option> <option value="32">Homes for Sale - Houses</option> <option value="33">Homes for Sale - Duplex</option> <option value="34">Homes for Sale - Townhouse</option> <option value="35">Homes for Sale - Condo</option> <option value="36">Homes for Sale - Mobile Homes</option> <option value="39">Housing Rentals - Apartments</option> <option value="40">Housing Rentals - Houses</option> <option value="41">Housing Rentals - Townhouse</option> <option value="42">Housing Rentals - Conda</option> <option value="43">Housing Rentals - Mobile Home</option> <option value="44">Housing Rentals - Duplex</option> <option value="45">Upcoming Events - Lethbridge</option> <option value="46">Upcoming Events - Coaldale</option> <option value="47">Upcoming Events - Coalhurst</option> <option value="48">Upcoming Events - Taber</option> <option value="49">Upcoming Events - Fort McLeod</option> <option value="50">Upcoming Events - Surrounding Areas</option> <option value="51">Automotive Parts and Accessories - Used Parts</option> <option value="52">Automotive Parts and Accessories - New Parts</option> <option value="53">Motorcycles - ATV</option> <option value="54">Motorcycle Parts and Accessories - Apparel</option> <option value="55">Motorcycle Parts and Accessories - Helmuts</option> <option value="56">Motorcycle Parts and Accessories - Boots</option> <option value="58">Motorcycle Parts and Accessories - Equipment and Tools </option> <option value="59">Motorcycle and ATV Parts - New</option> <option value="22">Motorcycle - OFF ROAD</option> <option value="60">Motorcycle Parts and Accessories - Used</option> <option value="61">Houses for Sale - Acreages</option> <option value="62">Farm / Livestock / Equipment - Tractors</option> <option value="63">Farm / Livestock / Equipment - Tillers</option> <option value="64">Farm / Livestock / Equipment - Mowers</option> <option value="65">Farm / Livestock / Equipment - Livestock</option> <option value="66">Farm / Livestock / Equipment - Feed</option> <option value="71">Housing Rentals - Acreages</option> <option value="72">Employment Ads - Professional</option> <option value="73">Employment Ads - Trades</option> <option value="74">Employment Ads - General</option> <option value="149">Community Meetings</option> <option value="148">Tools</option> <option value="140">Bicycles - Single Speed</option> <option value="141">Bicycles - 10 Speed and Under</option> <option value="142">Bicycles - 18 Speed</option> <option value="143">Bicycles - Over 18 Speed</option> <option value="144">Bicycles - Unicycles</option> <option value="145">Bicycles - Scooters</option> <option value="146">Bicycles - Childrens Trikes</option> <option value="147">Bicycles - Children Bikes with Training Wheels</option> <option value="23">Motorcycle - Street Legal</option> <option value="24">Recreational Vehicle - Tent Trailer</option> <option value="25">Recreational Vehicle - Trailers</option> <option value="26">Recreational Vehicle - Motor Homes</option> </select></font></p> <p><font face="Arial"><font size="2">Item Type </font> <font size="3"> <select size="1" name="item_type"> <option selected value="1">Auction</option> <option value="2">Dutch Auction</option> <option value="3">Fixed Price</option> <option value="4">Classified Ad</option> </select></font><font size="2"> Quantity </font> <font size="3"> <input type="text" name="quantity_available" size="4"></font><font size="2"> Starting Bid </font> <font size="3"><input type="text" name="starting_bid_price" size="8"></font><font size="2"> Bid Increment (auction only ) </font> <font size="3"> <input type="text" name="bid_increment" size="8"></font><font size="2"> Reserve Bid (auction only ) </font> <font size="3"> <input type="text" name="reserve_price" size="8"></font><font size="2"> </font> </font></p> <p><font face="Arial"><font size="2"># of Days (1-30)</font><font size="3"><input type="text" name="duration" size="4"></font><font size="2"> End Time : <select size="1" name="end_hour"> <option value="24">12:00 a.m.</option> <option value="1">1:00 a.m.</option> <option value="2">2:00 a.m.</option> <option value="3">3:00 a.m.</option> <option value="4">4:00 a.m.</option> <option value="5">5:00 a.m.</option> <option value="6">6:00 a.m.</option> <option value="7">7:00 a.m.</option> <option value="8">8:00 a.m.</option> <option value="9">9:00 a.m.</option> <option value="10">10:00 a.m.</option> <option value="11">11:00 a.m.</option> <option value="12">12:00 p.m.</option> <option value="13">1:00 p.m.</option> <option value="14">2:00 p.m.</option> <option value="15">3:00 p.m.</option> <option value="16">4:00 p.m.</option> <option value="17">5:00 p.m.</option> <option value="18">6:00 p.m.</option> <option value="19">7:00 p.m.</option> <option value="20" selected>8:00 p.m.</option> <option value="21">9:00 p.m.</option> <option value="22">10:00 p.m.</option> <option value="23">11:00 p.m.</option> </select> Auto Relist </font> <font size="3"><select size="1" name="auto_relist"> <option value="1" selected>Yes</option> <option value="0">No</option> </select> </font><font size="2"> City </font> <font size="3"> <input type="text" name="city" size="15"></font><font size="2"> Province/State </font> <font size="3"> <input type="text" name="state" size="17"></font><font size="2"> Country </font> <font size="3"><select size="1" name="country"> <option selected value="32">Canada</option> <option value="210">USA</option> </select></font><font size="2"> </font></font></p> <p><font face="Arial"><font size="2">Item Description </font> <font size="3"> <textarea rows="7" name="item_description" cols="162"></textarea></font></font></p> <p align="center"><font face="Arial"><font size="2"> Paypal ID (Optional)</font><font size="3"><input type="text" name="paypal_id" size="25"></font><font size="2"> Hit Counter</font><font size="3"><select size="1" name="hit_counter"> <option selected value="0">No Counter</option> <option value="1">Style 1</option> <option value="2">Style 2</option> <option value="3">Style 3</option> </select></font></font></p> <hr> <hr> <button name="Reset" type="reset">Reset</button> <p><b><font face="Arial" size="2">Please verify all details listed for your items before pressing the save button. </font></b></p> <p><font face="Arial" size="2">Return to <a href="http://www.forsale4u.ca"> www.forsale4u.ca</a> , sign in and go to sell. On the left side, <b> select Post Multiple Items</b> and select this file to upload.</font></p> <p><font face="Arial" size="2">After uploading this file, you will need to post uploaded items. <b>You are required to review each item before</b> proceeding to put a check mark beside them and selecting upload.</font></p> <p><font face="Arial" size="2">After they have been successfully posted, if you wish to add special features such as being displayed in a gallery, using bold print, etc. you will need to manually edit them.</font></p> <p><b><font face="Arial">Please verify this is your correct email address as this is where the completed form will be sent.</font></b></p> <input type="submit" name="Submit" /></html> Quote Link to comment https://forums.phpfreaks.com/topic/13857-getting-the-titles-in-a-csv-file-that-is-automatically-generated-in-php/#findComment-54079 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.