Create header names on the csv file from the $_POST values
I have a simple form that the contents are populated by pulling data from the database. I use a multi name option so I can loop through the data in PHP easily.
The form may expand in future so I do not want to reuse code in the PHP end. I am making a csv from the outputted data and want to create header names on the csv file from the $_POST array() I can access the data but it populates a new row in the csv file instead of a new column.I would also like to output the data from the posted data to new rows. I have tried a few things which I have commented in the code of the result I got when I tried them.
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
foreach ($_POST as $key => $value)
{
//This is where I want to add the $_POST values as headers
fputcsv($output, array($Key));//This is what I have tried but it just adds in a new line not column
}
//I have also tried the following for the rows to add the data into the csv file
for ( $i = 0; $i < count($_POST) - 0; ++$i)
{
foreach ($_POST as $key => $value)
{
fputcsv($output, array($Key[$i])); //This seems to only return the first character
}
}//End of for loop
?>
<!-- form is to be populated from a mysql data base in a table -->
<!-- This pulls the data from the database and loops throgh and displays the data. -->
<form action = "" method = "Post">
<?php while ($row = $prepped->fetch(PDO::FETCH_OBJ)) :?>
<input type="text" name="Firstname" value="<?php echo $row->Firstname ; ?>">
<input type="text" name="Email" value="<?php echo $row->Email ; ?>">
<input type="text" name="LastName" value="<?php echo $row->Lastname ; ?>">
<input type="text" name="Phone" value="<?php echo $row->Phone ; ?>">
<?php endwhile;?>
</form>
php html
add a comment |
I have a simple form that the contents are populated by pulling data from the database. I use a multi name option so I can loop through the data in PHP easily.
The form may expand in future so I do not want to reuse code in the PHP end. I am making a csv from the outputted data and want to create header names on the csv file from the $_POST array() I can access the data but it populates a new row in the csv file instead of a new column.I would also like to output the data from the posted data to new rows. I have tried a few things which I have commented in the code of the result I got when I tried them.
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
foreach ($_POST as $key => $value)
{
//This is where I want to add the $_POST values as headers
fputcsv($output, array($Key));//This is what I have tried but it just adds in a new line not column
}
//I have also tried the following for the rows to add the data into the csv file
for ( $i = 0; $i < count($_POST) - 0; ++$i)
{
foreach ($_POST as $key => $value)
{
fputcsv($output, array($Key[$i])); //This seems to only return the first character
}
}//End of for loop
?>
<!-- form is to be populated from a mysql data base in a table -->
<!-- This pulls the data from the database and loops throgh and displays the data. -->
<form action = "" method = "Post">
<?php while ($row = $prepped->fetch(PDO::FETCH_OBJ)) :?>
<input type="text" name="Firstname" value="<?php echo $row->Firstname ; ?>">
<input type="text" name="Email" value="<?php echo $row->Email ; ?>">
<input type="text" name="LastName" value="<?php echo $row->Lastname ; ?>">
<input type="text" name="Phone" value="<?php echo $row->Phone ; ?>">
<?php endwhile;?>
</form>
php html
2
$key
is not the same as$Key
. What should- 0
incount($_POST) - 0
do? "but it just adds in a new line" Create a line containing your columns and then write that line
– kerbholz
Nov 22 '18 at 11:40
I can do it manually by adding in the $_POST[''] like this but I would rather have it as automated as possible for ( $i = 0; $i < count($_POST['Firstname']) - 0; ++$i) { fputcsv($output, array($_POST['Firstname '][$i], $_POST['Phone'][$i] ,$_POST['Email'][$i])); }
– Brian Nowlan
Nov 22 '18 at 11:49
add a comment |
I have a simple form that the contents are populated by pulling data from the database. I use a multi name option so I can loop through the data in PHP easily.
The form may expand in future so I do not want to reuse code in the PHP end. I am making a csv from the outputted data and want to create header names on the csv file from the $_POST array() I can access the data but it populates a new row in the csv file instead of a new column.I would also like to output the data from the posted data to new rows. I have tried a few things which I have commented in the code of the result I got when I tried them.
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
foreach ($_POST as $key => $value)
{
//This is where I want to add the $_POST values as headers
fputcsv($output, array($Key));//This is what I have tried but it just adds in a new line not column
}
//I have also tried the following for the rows to add the data into the csv file
for ( $i = 0; $i < count($_POST) - 0; ++$i)
{
foreach ($_POST as $key => $value)
{
fputcsv($output, array($Key[$i])); //This seems to only return the first character
}
}//End of for loop
?>
<!-- form is to be populated from a mysql data base in a table -->
<!-- This pulls the data from the database and loops throgh and displays the data. -->
<form action = "" method = "Post">
<?php while ($row = $prepped->fetch(PDO::FETCH_OBJ)) :?>
<input type="text" name="Firstname" value="<?php echo $row->Firstname ; ?>">
<input type="text" name="Email" value="<?php echo $row->Email ; ?>">
<input type="text" name="LastName" value="<?php echo $row->Lastname ; ?>">
<input type="text" name="Phone" value="<?php echo $row->Phone ; ?>">
<?php endwhile;?>
</form>
php html
I have a simple form that the contents are populated by pulling data from the database. I use a multi name option so I can loop through the data in PHP easily.
The form may expand in future so I do not want to reuse code in the PHP end. I am making a csv from the outputted data and want to create header names on the csv file from the $_POST array() I can access the data but it populates a new row in the csv file instead of a new column.I would also like to output the data from the posted data to new rows. I have tried a few things which I have commented in the code of the result I got when I tried them.
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
foreach ($_POST as $key => $value)
{
//This is where I want to add the $_POST values as headers
fputcsv($output, array($Key));//This is what I have tried but it just adds in a new line not column
}
//I have also tried the following for the rows to add the data into the csv file
for ( $i = 0; $i < count($_POST) - 0; ++$i)
{
foreach ($_POST as $key => $value)
{
fputcsv($output, array($Key[$i])); //This seems to only return the first character
}
}//End of for loop
?>
<!-- form is to be populated from a mysql data base in a table -->
<!-- This pulls the data from the database and loops throgh and displays the data. -->
<form action = "" method = "Post">
<?php while ($row = $prepped->fetch(PDO::FETCH_OBJ)) :?>
<input type="text" name="Firstname" value="<?php echo $row->Firstname ; ?>">
<input type="text" name="Email" value="<?php echo $row->Email ; ?>">
<input type="text" name="LastName" value="<?php echo $row->Lastname ; ?>">
<input type="text" name="Phone" value="<?php echo $row->Phone ; ?>">
<?php endwhile;?>
</form>
php html
php html
edited Nov 22 '18 at 12:13
Julian Stark
1,2681526
1,2681526
asked Nov 22 '18 at 11:36
Brian NowlanBrian Nowlan
185
185
2
$key
is not the same as$Key
. What should- 0
incount($_POST) - 0
do? "but it just adds in a new line" Create a line containing your columns and then write that line
– kerbholz
Nov 22 '18 at 11:40
I can do it manually by adding in the $_POST[''] like this but I would rather have it as automated as possible for ( $i = 0; $i < count($_POST['Firstname']) - 0; ++$i) { fputcsv($output, array($_POST['Firstname '][$i], $_POST['Phone'][$i] ,$_POST['Email'][$i])); }
– Brian Nowlan
Nov 22 '18 at 11:49
add a comment |
2
$key
is not the same as$Key
. What should- 0
incount($_POST) - 0
do? "but it just adds in a new line" Create a line containing your columns and then write that line
– kerbholz
Nov 22 '18 at 11:40
I can do it manually by adding in the $_POST[''] like this but I would rather have it as automated as possible for ( $i = 0; $i < count($_POST['Firstname']) - 0; ++$i) { fputcsv($output, array($_POST['Firstname '][$i], $_POST['Phone'][$i] ,$_POST['Email'][$i])); }
– Brian Nowlan
Nov 22 '18 at 11:49
2
2
$key
is not the same as $Key
. What should - 0
in count($_POST) - 0
do? "but it just adds in a new line" Create a line containing your columns and then write that line– kerbholz
Nov 22 '18 at 11:40
$key
is not the same as $Key
. What should - 0
in count($_POST) - 0
do? "but it just adds in a new line" Create a line containing your columns and then write that line– kerbholz
Nov 22 '18 at 11:40
I can do it manually by adding in the $_POST[''] like this but I would rather have it as automated as possible for ( $i = 0; $i < count($_POST['Firstname']) - 0; ++$i) { fputcsv($output, array($_POST['Firstname '][$i], $_POST['Phone'][$i] ,$_POST['Email'][$i])); }
– Brian Nowlan
Nov 22 '18 at 11:49
I can do it manually by adding in the $_POST[''] like this but I would rather have it as automated as possible for ( $i = 0; $i < count($_POST['Firstname']) - 0; ++$i) { fputcsv($output, array($_POST['Firstname '][$i], $_POST['Phone'][$i] ,$_POST['Email'][$i])); }
– Brian Nowlan
Nov 22 '18 at 11:49
add a comment |
2 Answers
2
active
oldest
votes
$keys=array_keys($_POST);
fputcsv($output, array_keys($_POST));
for ($i = 0; $i < count($_POST); $i++)
{
$result=array();
foreach($keys as $key => $value)
{
$result=$_POST[$value][$i];
}
fputcsv($output, $result);
}
exit();
You will get key as column name and value as row
Thank you so much saved me so much time
– Brian Nowlan
Nov 22 '18 at 12:12
add a comment |
$keys=array_keys($_POST);
fputcsv($output, array_keys($_POST));
do {
$result=array();
$i++;
foreach($keys as $key => $value){
$result=$_POST[$value][$i];
}
fputcsv($output, $result);
}
while ($i = 0; $i < count($_POST));
exit();
Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Kurt Van den Branden
Nov 22 '18 at 14:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430111%2fcreate-header-names-on-the-csv-file-from-the-post-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$keys=array_keys($_POST);
fputcsv($output, array_keys($_POST));
for ($i = 0; $i < count($_POST); $i++)
{
$result=array();
foreach($keys as $key => $value)
{
$result=$_POST[$value][$i];
}
fputcsv($output, $result);
}
exit();
You will get key as column name and value as row
Thank you so much saved me so much time
– Brian Nowlan
Nov 22 '18 at 12:12
add a comment |
$keys=array_keys($_POST);
fputcsv($output, array_keys($_POST));
for ($i = 0; $i < count($_POST); $i++)
{
$result=array();
foreach($keys as $key => $value)
{
$result=$_POST[$value][$i];
}
fputcsv($output, $result);
}
exit();
You will get key as column name and value as row
Thank you so much saved me so much time
– Brian Nowlan
Nov 22 '18 at 12:12
add a comment |
$keys=array_keys($_POST);
fputcsv($output, array_keys($_POST));
for ($i = 0; $i < count($_POST); $i++)
{
$result=array();
foreach($keys as $key => $value)
{
$result=$_POST[$value][$i];
}
fputcsv($output, $result);
}
exit();
You will get key as column name and value as row
$keys=array_keys($_POST);
fputcsv($output, array_keys($_POST));
for ($i = 0; $i < count($_POST); $i++)
{
$result=array();
foreach($keys as $key => $value)
{
$result=$_POST[$value][$i];
}
fputcsv($output, $result);
}
exit();
You will get key as column name and value as row
answered Nov 22 '18 at 12:08
Lavanya ELavanya E
1478
1478
Thank you so much saved me so much time
– Brian Nowlan
Nov 22 '18 at 12:12
add a comment |
Thank you so much saved me so much time
– Brian Nowlan
Nov 22 '18 at 12:12
Thank you so much saved me so much time
– Brian Nowlan
Nov 22 '18 at 12:12
Thank you so much saved me so much time
– Brian Nowlan
Nov 22 '18 at 12:12
add a comment |
$keys=array_keys($_POST);
fputcsv($output, array_keys($_POST));
do {
$result=array();
$i++;
foreach($keys as $key => $value){
$result=$_POST[$value][$i];
}
fputcsv($output, $result);
}
while ($i = 0; $i < count($_POST));
exit();
Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Kurt Van den Branden
Nov 22 '18 at 14:30
add a comment |
$keys=array_keys($_POST);
fputcsv($output, array_keys($_POST));
do {
$result=array();
$i++;
foreach($keys as $key => $value){
$result=$_POST[$value][$i];
}
fputcsv($output, $result);
}
while ($i = 0; $i < count($_POST));
exit();
Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Kurt Van den Branden
Nov 22 '18 at 14:30
add a comment |
$keys=array_keys($_POST);
fputcsv($output, array_keys($_POST));
do {
$result=array();
$i++;
foreach($keys as $key => $value){
$result=$_POST[$value][$i];
}
fputcsv($output, $result);
}
while ($i = 0; $i < count($_POST));
exit();
$keys=array_keys($_POST);
fputcsv($output, array_keys($_POST));
do {
$result=array();
$i++;
foreach($keys as $key => $value){
$result=$_POST[$value][$i];
}
fputcsv($output, $result);
}
while ($i = 0; $i < count($_POST));
exit();
edited Nov 22 '18 at 12:49
cakan
1,56432331
1,56432331
answered Nov 22 '18 at 12:29
Vasu SharmaVasu Sharma
12
12
Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Kurt Van den Branden
Nov 22 '18 at 14:30
add a comment |
Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Kurt Van den Branden
Nov 22 '18 at 14:30
Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Kurt Van den Branden
Nov 22 '18 at 14:30
Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
– Kurt Van den Branden
Nov 22 '18 at 14:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430111%2fcreate-header-names-on-the-csv-file-from-the-post-values%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
$key
is not the same as$Key
. What should- 0
incount($_POST) - 0
do? "but it just adds in a new line" Create a line containing your columns and then write that line– kerbholz
Nov 22 '18 at 11:40
I can do it manually by adding in the $_POST[''] like this but I would rather have it as automated as possible for ( $i = 0; $i < count($_POST['Firstname']) - 0; ++$i) { fputcsv($output, array($_POST['Firstname '][$i], $_POST['Phone'][$i] ,$_POST['Email'][$i])); }
– Brian Nowlan
Nov 22 '18 at 11:49