Jump to content

Printing Dynamic Input Results to CSV


theodore_steiner

Recommended Posts

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);
}
Link to comment
Share on other sites

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",zxcvbnm
then 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",zxcvbnm
or simply using multiple rows

qwertyuiop,asdfghjkl,one,zxcvbnm
qwertyuiop,asdfghjkl,two,zxcvbnm
qwertyuiop,asdfghjkl,three,zxcvbnm
or 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)
Link to comment
Share on other sites

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 by theodore_steiner
Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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 by theodore_steiner
Link to comment
Share on other sites

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);
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.