[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PHP- Writing $_Post array to CSV

Posted on 2013-11-01
5
Medium Priority
?
630 Views
Last Modified: 2013-11-30
Afternoon
I hope you can help me, I am new to PHP,

I have posted an Array to a .php file as below.

Array
(
    [id] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [S1] => Array
        (
            [0] => AW01
            [1] => AW01
            [2] => AW02
            [3] => AW03
        )

    [S2] => Array
        (
            [0] => 1234
            [1] => 4567
            [2] => 9874
            [3] => 1265
        )

    [S3] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 2
            [3] => 5
        )

    [S5] => Array
        (
            [0] =>
            [1] => yui
            [2] =>
            [3] =>
        )

    [S6] => Array
        (
            [0] =>
            [1] =>
            [2] =>
            [3] =>
        )

    [S4] => Array
        (
            [0] => 1
            [1] => 1
        )

    [FormSubmit] => Submit
)

I want to write the Array to a CSV file So i can read it as

ID                S1                   S2                  S3                      S4              S5                 S6
1                 AW01            1234                 1                     1                  yui
2                  AW01            4567               1                       1                
3 etc
4 etc
I hope you can help as i have been going through this for Days and days without any luck.
Thanks in Advance.
0
Comment
Question by:Q_evans
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39617399
Have a read through this and see how you get on:

<?php
		
//the data
$data = array(
	'id' => array(1,2,3,4),
	's1' => array('AW01','AW01','AW02','AW03'),
	's2' => array('1234', '4567', '9874', '1265'),
	's3' => array(1,1,2,5),
	's4' => array(1,1),
	's5' => array(null, 'yui',null,null),
	's6' => array(null, null, null, null),
);		

//get the row headers
foreach ($data as $key => $info):
	$rowHeaders[] = $key;
endforeach;

//count the rows
$count = count($data['id']);

//loop through the data
for ($i=0; $i < $count; $i++) {
	//temp place holder for each row	
	$rowData = array();
	//get the right index from the right key
	foreach ($data as $key => $info):
		//make sure it's set before trying to add it
		if (isset($data[$key][$i])) $rowData[] = $data[$key][$i];
	endforeach;
	
	$rows[] = $rowData;
};

//open a file for writing
$file = fopen("data.csv","w");

//put the header row
fputcsv($file,$rowHeaders);

//loop through the records
foreach ($rows as $row):
	//write the record
	fputcsv($file,$row);	
endforeach;

//close the file
fclose($file);

?>

Open in new window

0
 

Author Comment

by:Q_evans
ID: 39617420
Hi Chris Thanks for that.

The Posted Array will change everytime as it is taken from a table from the page before.

So yes this works great, although I need the Arrays from the $_Post array. to be wirtten to the CSV? I hope this makes sense.

Regards
0
 
LVL 44

Accepted Solution

by:
Chris Stanyon earned 1200 total points
ID: 39618659
OK. If the format of the POST array stays the same, then you just need to change $data to $_POST. I hardcoded it for testing purposes as I don't have your HTML Form:

<?php
//get the row headers
foreach ($_POST as $key => $info):
	$rowHeaders[] = $key;
endforeach;

//count the rows
$count = count($_POST['id']);

//loop through the data
for ($i=0; $i < $count; $i++) {
	//temp place holder for each row	
	$rowData = array();
	//get the right index from the right key
	foreach ($_POST as $key => $info):
		//make sure it's set before trying to add it
		if (isset($_POST[$key][$i])) $rowData[] = $_POST[$key][$i];
	endforeach;
	
	$rows[] = $rowData;
};

//open a file for writing
$file = fopen("data.csv","w");

//put the header row
fputcsv($file,$rowHeaders);

//loop through the records
foreach ($rows as $row):
	//write the record
	fputcsv($file,$row);	
endforeach;

//close the file
fclose($file);

?>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39618907
$_Post is probably not correct.  Variable names are case-sensitive in PHP, so you probably want $_POST.

If you want to show us the HTML form that is used to gather the data we might be able to test a little better.
0
 

Author Comment

by:Q_evans
ID: 39619204
Thanks ChrisStanyon, Works a treat...
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question