separate csv files for each response...

User 2716615 Photo


Registered User
23 posts

Thanks again Brian (and Eric) I had searched the forums for info on $session variables but read that they were cancelled on redirect. I had not seen the thread you highlighted above, so I will investigate - not sure which bit of code to change mind you!!

In the mean time I took your suggestion of reading from the file itself before renaming it. I have been learning some PHP! First I used file_get_contents which works but is a bit rough.

Then I read about explode and fgetcsv and decided to try the latter as it promised an array of all the data.

So I've managed to come up with this and it works! It also echoes all the fields and the data to the 'Thank you' page too (though that bit is not very pretty at the moment!) and it renames my file '$data[1]$data[0].csv' which with my form means it names each file [username][ordernumber].csv (by the way, the 'ordernumber' field comes from adapting one of Eric's great js scripts - thanks Eric!)

<html>
</body>
//pretty stuff here!
Thanks! Your form is on the way!
</body>
</html>

<?php
//main loop adapted PHP manual fgetcsv example
$row = 1;
if (($handle = fopen("/PATH_TO/storage/csv/form-results.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
//echo the form data to page
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c]." ";
//my data is on row 2 so 'if' that
if ($row=2) {
global $myNewName;
//new name constructed here
$myNewName = $data[1].$data[0].'.csv';
}
}
}
// close file
fclose($handle);
}
//naming function
function myNewFileName() {
global $myNewName;
//original Brian code!
$old_file_name = '/PATH_TO/storage/csv/form-results.csv';
$new_file_name = '/PATH_TO/storage/csv/'.$myNewName;
rename($old_file_name, $new_file_name);
}
// call naming function
myNewFileName();
?>


(Change the PATH_TO bits obvs.)

Great! this means I have all the data back as variables without session but I will definitely look into the $session idea if we can find the right bit of code to change!

Thanks for your guidance here and elsewhere on this site Brian and Eric. I hope my end result will be helpful to others - perhaps someone can work out how to make the formatting pretty even when there are empty fields! :)

Rob.
User 187934 Photo


Senior Advisor
20,200 posts

Rob, I did some checking and the variables aren't unset on redirect. Check this out.
http://ericrohloff.com/coffeecup/ccforu … index.html
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2716615 Photo


Registered User
23 posts

Wow, that's great. Could you show me how you got there? :)

thanks,

Rob.
User 187934 Photo


Senior Advisor
20,200 posts

I'm going to continue this conversation here.
http://www.coffeecup.com/forums/web-for … post239628
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2716615 Photo


Registered User
23 posts

Thanks Eric.

Note re above - if you don't want to rename the CSV file as I am doing here, you can use the top part of the code above to read your entire 'form-results.csv' and use it to query the data, like a simple database. Just a thought.

Rob.


Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.