theodore_steiner Posted October 20, 2016 Share Posted October 20, 2016 Hi All, I am very new to PHP and I need some help. I've got a multipage form that is made of up straight inputs and inputs that can be created dynamically, on the fly, so people can add in other jobs/degrees/awards. When the user submits, I am trying to load the input results to a .csv file. For the input fields that are not dynamically created this is no problem, but for the ones that are, an I've got it generating an array of values. However, when I pass these values to the csv sheet each new array entry value is taking up the space next to it. Is it possible to have the first set of array values take up the requisite fields and then have the remaining values relative to that particular array section appear below, almost as if you put in a <br> tag? PHP Code: $firstName = $_POST["firstName"]; $lastName = $_POST["lastName"]; $homeAddress = $_POST["homeAddress"]; $homeAddressTwo = $_POST["homeAddressTwo"]; $city = $_POST["city"]; $province = $_POST["province"]; $postalCode = $_POST["postalCode"]; $homePhone = $_POST["homePhone"]; $personalEmail = $_POST["personalEmail"]; $confirmEmail = $_POST["confirmEmail"]; $oectaNumber = $_POST["oectaNumber"]; $memberStatus = $_POST["memberStatus"]; $teacherTraining = $_POST["teacherTraining"]; $teachingYears = $_POST["teachingYears"]; $employmentHistory = $_POST["employmentHistory"]; $employmentHistoryValues = print_r(array_values($employmentHistory)); $csvdata = $firstName . ", " . $lastName . ", " . $homeAddress . ", " . $homeAddressTwo . ", " . $city . ", " . $province . ", " . $postalCode . ", " . $homePhone . ", " . $personalEmail . ", " . $confirmEmail . ", " . $oectaNumber . ", " . $memberStatus . ", " . $teacherTraining . ", " . $teachingYears . "," . $employmentHistoryValues; $fp = fopen("formdata.csv", "a"); if($fp) { fwrite($fp, $csvdata . "\n"); fclose($fp); } Quote Link to comment https://forums.phpfreaks.com/topic/302368-printing-dynamic-input-results-to-csv/ Share on other sites More sharing options...
requinix Posted October 20, 2016 Share Posted October 20, 2016 Is it possible to have the first set of array values take up the requisite fields and then have the remaining values relative to that particular array section appear below, almost as if you put in a tag? If you mean like qwertyuiop,asdfghjkl,one,zxcvbnm two three then no because then it wouldn't be CSV anymore. Another version would be qwertyuiop,asdfghjkl,"one two three",zxcvbnmthen you can try that but there's no guarantee a CSV reader will understand it. How about combining the values like qwertyuiop,asdfghjkl,"one,two,three",zxcvbnmor simply using multiple rows qwertyuiop,asdfghjkl,one,zxcvbnm qwertyuiop,asdfghjkl,two,zxcvbnm qwertyuiop,asdfghjkl,three,zxcvbnmor using two CSV files, one with the base data and another with the lists. qwertyuiop,asdfghjkl,zxcvbnm qwertyuiop,one qwertyuiop,two qwertyuiop,three(pretending that "qwertyuiop" is a unique identifier) Quote Link to comment https://forums.phpfreaks.com/topic/302368-printing-dynamic-input-results-to-csv/#findComment-1538431 Share on other sites More sharing options...
theodore_steiner Posted October 20, 2016 Author Share Posted October 20, 2016 (edited) Combining the values would work perfectly. So would multiple rows. But what I was afraid of was lets say user1 puts in 2 inputs dynamically, and then user2 puts in 3inputs dynamically, won't the csv get confused as to which row belongs to which set of user input data? I'm sorry to press, but how would I go about doing one or both of those methods? Edited October 20, 2016 by theodore_steiner Quote Link to comment https://forums.phpfreaks.com/topic/302368-printing-dynamic-input-results-to-csv/#findComment-1538432 Share on other sites More sharing options...
requinix Posted October 20, 2016 Share Posted October 20, 2016 The only way the CSV would "get confused" is if your code did something weird when generating the CSV data. So... don't do anything weird. Two values? Three values? One value? implode() them to get the comma-separated string, put that into an array with the other values, and use fputcsv to write to the file (instead of building the line yourself and writing using fwrite). $line_of_csv_data = array( "qwertyuiop", "asdfghjkl", implode(",", $array_of_values), "zxcvbnm" ); fputcsv($fp, $line_of_csv_data); Quote Link to comment https://forums.phpfreaks.com/topic/302368-printing-dynamic-input-results-to-csv/#findComment-1538433 Share on other sites More sharing options...
theodore_steiner Posted October 20, 2016 Author Share Posted October 20, 2016 (edited) Thank you so very much for the help! I promise I won't do anything weird! One last question, the fputcsv($fp, $line_of_csv_data) line of code, wouldn't appear in the code below but after yes? $csvdata = $firstName . ", " . $lastName . ", " . $homeAddress . ", " . $homeAddressTwo . ", " . $city . ", " . $province . ", " . $postalCode . ", " . $homePhone . ", " . $personalEmail . ", " . $confirmEmail . ", " . $oectaNumber . ", " . $memberStatus . ", " . $teacherTraining . ", " . $teachingYears . "," . $employmentHistoryValues; $fp = fopen("formdata.csv", "a"); if($fp) { fwrite($fp, $csvdata . "\n"); fclose($fp); } Edited October 20, 2016 by theodore_steiner Quote Link to comment https://forums.phpfreaks.com/topic/302368-printing-dynamic-input-results-to-csv/#findComment-1538434 Share on other sites More sharing options...
requinix Posted October 20, 2016 Share Posted October 20, 2016 It replaces the $csvdata line and the fwrite. That work you did about building up the line of CSV data and writing it to the file? fputcsv can do all the work for you if you provide it with an array of all the values you want to write. $csvdata = array( $firstName, $lastName, $homeAddress, $homeAddressTwo, $city, $province, $postalCode, $homePhone, $personalEmail, $confirmEmail, $oectaNumber, $memberStatus, $teacherTraining, $teachingYears, $employmentHistoryValues ); // I don't know where the thing with multiple values is - whether it's one of those variables // or whether you haven't written it into the code yet // whatever the answer, you take that array, implode() it to a single value, and put it in $csvdata // like // $csvdata = array( // ..., // implode(",", $array_with_multiple_values), // ... // ); $fp = fopen("formdata.csv", "a"); if($fp) { fputcsv($fp, $csvdata); fclose($fp); } Quote Link to comment https://forums.phpfreaks.com/topic/302368-printing-dynamic-input-results-to-csv/#findComment-1538437 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.