Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Andrew Folts
Andrew Folts
14,238 Points

How would you create a CSV file using a php array?

I have a Wordpress project where there's a survey app and at the end the client wants to receive a CSV file with the answers.

A simple example:

  • Name: John Smith
  • Position: Manager
  • Date: 1/20/17

From the php docs, I think would do the following:

$rows = array (
    array('Name', 'Position', 'Date'),
    array('John Smith', 'Manager', '1/20/17')
);

$fp = fopen('file.csv', 'w');

foreach ($rows as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);

What I'm not sure about is would I have an empty CSV saved somewhere on the site that I write to, save, and attach to the email every time?

Can php write to a file saved in Wordpress uploads... or is there a smarter way to do this?

Thanks!

2 Answers

Hi Andrew,

Have you thought about using a form builder like Ninja Forms, with this you can use an add-on that will allow for front end posting to a custom post type e.g. Survey.

So you would build out the form to suit the fields of your Survey post type, a user can complete the form on the front end and when they submit it saves this as a post of your custom post type( Survey ).

If this was set up this way, you would have all survey responses saved as a post and you would be able to export them using something link WP All Export.

This is by no means an easy WordPress thing but a good skill to have if you go down this route :)

Hope this helps

Craig :)

Andrew Folts
Andrew Folts
14,238 Points

Yea, unfortunately the app is quite complex and interactiveβ€”Ninja Forms wouldn't cover everything. Plus, the client doesn't want to have to log in, they just want an email with a CSV.

Storing the data in a custom post type is not a bad idea though.

Hi Andrew,

I think if you store the data as a post type you are better placed to work with once in the database.

Also this way you can potentially hook into wp_update_post and run a function every time a new post is submitted.

Another plus to the post type is that you could look to export the total collected surveys at any point should your client request so.

Personally I would need to be doing just as much figuring out and googling as you may have been because I have not done this before, however if you are able to allow for costs on such a project Codeable is always a good place to look for help!

Hope this helps a little on the way to getting things going.

Craig :)

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

To create a CSV file, you should use fputcsv function (more here)

In case if you need to read from CSV file, I suggest you to use this library from PHP League