Create header names on the csv file from the $_POST values





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















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>









share|improve this question




















  • 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













  • 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


















3















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>









share|improve this question




















  • 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













  • 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














3












3








3








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>









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 12:13









Julian Stark

1,3111527




1,3111527










asked Nov 22 '18 at 11:36









Brian NowlanBrian Nowlan

185




185








  • 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













  • 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





    $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








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












2 Answers
2






active

oldest

votes


















2














$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






share|improve this answer
























  • Thank you so much saved me so much time

    – Brian Nowlan
    Nov 22 '18 at 12:12



















-1














$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();





share|improve this answer


























  • 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












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
});


}
});














draft saved

draft discarded


















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









2














$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






share|improve this answer
























  • Thank you so much saved me so much time

    – Brian Nowlan
    Nov 22 '18 at 12:12
















2














$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






share|improve this answer
























  • Thank you so much saved me so much time

    – Brian Nowlan
    Nov 22 '18 at 12:12














2












2








2







$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






share|improve this answer













$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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 12:08









Lavanya ELavanya E

1498




1498













  • 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





Thank you so much saved me so much time

– Brian Nowlan
Nov 22 '18 at 12:12













-1














$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();





share|improve this answer


























  • 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
















-1














$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();





share|improve this answer


























  • 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














-1












-1








-1







$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();





share|improve this answer















$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();






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 12:49









cakan

1,56632432




1,56632432










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



















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?