Forums

Handling data / arrays in multipage form

davidakis 06 Aug, 2013
Hi to all,
I'm creating a multipage form, and in the second page I created a set of inputs (up to 99 sets) following the "add - one" method suggested by Bob. Now I have some questions about how to handle data.

I wish to create a table (or a .csv file) which contains both data got from first and from second step; I wish also to send email confirmation with all data. How can I do it?

Thanks to all
Davide
davidakis 07 Aug, 2013
Update: for the first step I decided to save the data in a DB; for the second I'm thinking about using a Custom Code action. The snippet here below sets up the input field for surname:

echo "<div id='recipient_{$i}' class='{$class}' >
  <div class='multiline_start'>
    <label>Cognome</label><br />
    <input type='text' name='recipient[{$i}][cognome]' id='recipient_{$i}_cognome' />
  </div>


That means I have got a multidimensional array in which "recipient[{$i}]" contains [cognome],[nome] and so on. So I hade this idea of processing all data:

foreach $recipient as $n => $val {...}
Is that right?

Thanks
Davide
GreyHead 13 Aug, 2013
Hi davidakis,

Yes that looks OK to me, you need some extra brackets though:
foreach  ( $recipient as $n => $val ) {
   $val['name'] //...;
}

Bob
davidakis 14 Aug, 2013
Update: I ended building my form, but it doesn't work as expected. It doesn't send email, and it doesn't create the excel file. Here below the snippet I used to handle data:

<?php
$filename="elencoiscritti.xls"; /* filename, excel type */
   header ("Content-Type: application/vnd.ms-excel");
   header ("Content-Disposition: inline; filename=$filename");
?>
...
/* html tags to define body and table */
<?
/* Includes all fields from the first page */

echo "<tr>";
echo "<td>";
echo $form->data['societa']; ."</td>"
echo "</tr>";

if ($form->data['soc_2']<>"") { /* includes this field only if is not empty */
echo "<tr>";
echo "<td>";
echo $form->data['soc_2']; ."</td>"
echo "</tr>";
}

... /* same method for other fields in the first page */


/* includes all fields from second page */

foreach ($recipient as $n => $val) { 
   
   echo "<tr>";
   echo "<td>" . $val['cognome'] . "</td>"
   ... /* same method for all the other fields */
   echo "</tr>";
   
}
?>
/*... closing html tags */


I used the foreach method also in the email template.
Thanks for any suggestions

Ps: I'm going to add missing semicolons...
GreyHead 14 Aug, 2013
Hi davidakis,

I don't know what you expect this to do. I suspect that once you change the header ChronoForms loses control. And why would you send an HTML table to an Excel file?

Bob
davidakis 14 Aug, 2013
I would like to process my data in order to give the user the possibility to save an .xls (or .csv, or whatever) file on his desktop with all the results; I would also like to send a mail with the same data. Actually I corrected 'the semicolon mistake', but the form behaves the same way: neither it sends the email confirmation, nor it saves any file. It only saves the 'first page' data in a DB, by using the DB Save action.

ps.: In effect after submitting the second page, the result is that: form reloads the second page with all 99 sets of input... So I think you're right, and I think I'll try another method.
About Email: My user is configured with no editor, while on Global settings I chose one.
davidakis 17 Aug, 2013
Update: I changed method by creating a .csv file (Custom code), but actually it works only with 'first step data'; the same happens in sending emails. Here below a piece of the snippet involved:

<?php
/*creates file */
$filename="includes/elencoiscritti.csv";    
$handle = fopen($filename, "w");

/* 'first step data' */

...

fwrite($handle, $data); /* scrive la prima parte dei dati */
fclose($handle);

$handle = fopen($filename, "a"); /* reopens file adding second step */

/* cicle for creating list */

foreach ($recipient as $n => $val) { /* comma separated data */
    
   $data2[] = $val['cognome'] . ",";
   ...
   $data2[] = $val['sesso'] . "\n"; /* last column */
   
fwrite($handle, $data2);   
}
fclose($handle);
echo "<br />" . "File csv salvato. Scaricalo in ftp.";
?>


I tried to do only one fwrite/fclose, the result is the same; I dragged a debugger and I found that my array is ok...
Thanks
davidakis 17 Aug, 2013
I don't know how to use it with my Custom element made of x sets of input...
GreyHead 17 Aug, 2013
Hi Davidakis,

You can use PHP to put your data into a flat array in the $form->data array - which you will need for a CSV export, then use the Data Path entry in the CSV Export [GH] action to point to that entry. It should work OK as long as the data sets aren’t enormous.

How do you want the data to appear in your spreadsheet? That will determine how you structure the flat array i.e. what are the column and row headers?

Bob
davidakis 17 Aug, 2013
Actually the columns are:

"Cognome" (i.e. Surname)
"Nome" (i.e. Name)
"Anno" (i.e. Year, intended as birth year)
"Nazionalità" (i.e. country of origin, selected by a dropdown menu)
"Sesso" (i.e. gender, selected by radio button)

There is no row header, but this could be made simply by putting an ID number for any row, as I don't know how many rows a customer will complete. (It could be up to 99).

So, doing a summary, my CSV file should appear as follows:

'first step data' -> one field, one row
'second step data' -> one set of fields, one row
GreyHead 18 Aug, 2013
Hi davidakis,

You lost me here:

So, doing a summary, my CSV file should appear as follows:

'first step data' -> one field, one row
'second step data' -> one set of fields, one row

I don't understand what that would look like? I don't think that you can have two different layouts in one CSV file :-(

Bob
davidakis 19 Aug, 2013
Ok Bob,
I'm going to try to solve my issue in one of these ways:

1 - exporting in .xls, that should permit to respect my layout;

2 - formatting 'first step data' in the same way as 'second step data' as they could be displayed in five columns.

I'll let you know if I need more help.

Thanks a lot
Davide
davidakis 19 Aug, 2013
Update:

I'm trying to use .Csv export [GH, ver 2]; I tried to save my 'second step data' in flat arrays by changing my custom element in this way:

echo "<div id='recipient_{$i}' class='{$class}' >
  <div class='multiline_start'>
    <label>Cognome</label><br />
    <input type='text' name='cognome[{$i}]' id='recipient_{$i}_cognome' />
</div>


That should create an array named "cognome"; I do the same with the other four fields.
Now the point is that the form creates the csv file after clicking on "Continua", before showing 'second step'. I tried to change multipage setup in this way:

On submit I put a Multipage and a Show html action. This one is configured (Advanced tab) to load 2nd page on submission event. I actually got the same result.

...This form is making me fool...
davidakis 21 Aug, 2013
I decided to turn multipage form in a 'tabbed form' using container. Now I have only to configure correctly my "On Submit" actions. 'First Step data' are saved in a DB, 'second step' are in arrays (Custom element with 'add one' method). I dragged the CSV Export (v 2.0 GH) action, but I don't know how to configure it for the custom element (each field has got is own array).

Thanks
Davide
davidakis 21 Aug, 2013
Good news: at last I solved all my issues by using containers, creating tabbed form, and exporting my data in a .csv file created by means of custom code. I was able, also to customize my Email Template, including both "first step" and "second step data" with a little php snippet inside the template box.

As this is "how to" section (I wrongly used it to pose questions) I think it'll be useful to update this post, rename it and eventually post my "how-to"🙂

Thanks far all support
Davide
GreyHead 21 Aug, 2013
Hi Davidakis,

To do a clean export to a CSV file I would expect that you need your inputs named like:
<input type='text' name='user_data[{$i}][name]' . . . >
<input type='text' name='user_data[{$i}][email]' . . . >
...

That would give a result like
data =>
  ['user_data'] =>
    [0] =>
       [name] => xxx 
       [email] => yyy
    [1] =>
       [name] => aaa 
       [email] => bbb
. . .

Then you would export $form->data['user_data'] to the CSV Export action.

Please drag a Debugger action into the On Submit event, then submit the form and post the debug results here so that I can see what data you now have.

Bob
davidakis 21 Aug, 2013
Here below the results (I replaced with 'xxx' some personal data):

Data Array:

Array
(
    [option] => com_chronoforms
    [tmpl] => component
    [chronoform] => Elenchi_societa
    [event] => submit
    [Itemid] => 
    [chrono_verification] => UNYBc
    [Invia] => Invia
    [44989b89e9226eef7a16392428d80cef] => 1
    [societa] => A.S. ATLETICA NEPI
    [soc_2] => 
    [email] => xxx@gmail.com
    [telefono] => xxx
    [affiliazione] => ACSI
    [cognome] => Array
        (
            [1] => Iandoli
            [2] => Iandoli
            ...
    [nome] => Array
        (
            [1] => Davide
            [2] => Sabrina
            ...
        )

    [anno] => Array
        (
            [1] => 1975
            [2] => 1971
            ...
        )

    [naz] => Array
        (
            [1] => I - Italia 
            [2] => I - Italia 
            ... 
        )

    [sesso] => Array
        (
            [1] => M
            [2] => F
        )

)

Validation Errors:

Array
(
)

Grazie per aver compilato il modulo. A breve riceverai una mail a conferma dei dati inseriti.
Vuoi iscrivere un altro gruppo? Fai click qui
Debug Data

    Core Captcha
        Passed the core captcha check!
    email
        8
            Result An email has been SENT successfully from (Iscrizioni Orme di Enea)xxx@gmail.com to xxx@gmail.com,xxx@gmail.com
            Body
            Società 	A.S. ATLETICA NEPI
            altra società 	
            E-mail 	xxxxxxx
            Telefono 	xxxxxxx
            Affiliazione 	ACSI
            Num.	Cognome	Nome	Anno di nascita	Nazionalità	Sesso
            1	Iandoli	Davide	1975	I - Italia 	M
            2	Iandoli	Sabrina	1971	I - Italia 	F


            Submitted by 88.49.239.146
            Attachments array ( )
GreyHead 21 Aug, 2013
Hi davidakis,

If you compare your results with my example then you should see the problem. You want the data to export in a structured sub array.

Bob
davidakis 21 Aug, 2013
Hi Bob, actually I have two types of data: the first is 'group data' which is email, sports society name, and so on; the second is a list of individuals who are part of this group. At last I need to save all my data in a file, and my problem was distinguish the group from the list of individuals.

If I got clearly I could get both 'group data' and 'list of individuals' by using your Csv export action and setting the table for the 'group', then writing $form->data['user_data'] in the Data Path box.

Thanks
GreyHead 02 Sep, 2013
Hi davidakis,

I think that a single CSV file can only contain a single data structure so you'd have to use two separate actions to export two files. Or you could look at using a PHP Excel library to create an Excel file with the compound layout.

Bob
davidakis 03 Sep, 2013
Hi Bob,
Eventually I realised I need my csv file only as an administrator: this gives to me the opportunity to download each file without copying the text of every mail message. I send to users a confirmation mail with both 'group data' and 'list of individuals'; the data in it are viewed in a compound way (as I need). I did that by a little PHP snippet inside the email template.

This way users have a kind of "bill" of their submission, in their mail message. That was what I wanted to do for users.

I get my csv file by processing data with Custom Code.

Thanks a lot for all your support.
Davide
GreyHead 03 Sep, 2013
Hi Davide,

Great - sounds as though you have it all working well enough :-)

Bob
davidakis 03 Sep, 2013
Yeah Bob,
Actually I designed the email template in this way: 'group data' are listed 'normally', as there is an input field for each data I need; by PHP snippet first I build the table headers, then, by a
foreach ($form->data['array'] as $n => $val) { ...
cycle I build each row of the table until I find an empty key of my $form->data['array'].

The custom code is very similar to this, the major difference is that it 'saves' the 'group data' in a row, then the 'list of individuals' are saved in the rows below that. Each file as a unique name, due to date/time and to IP address. 😀
This topic is locked and no more replies can be posted.