Forums

Best way to save repeater area data in a database table

farscape 13 Jan, 2018
I have a form that uses a repeater area, and want to save it in a database table. When I use the Create Table function, the default column names include the reference to the iteration key, but the data does not save. I cannot find anything in the manual or forum that explains how you save the repeater data. I assume that there is no option to normalize the data in multiple tables.

Do I I simply need to create columns for each iteration? That is, if my form has firstname{var:area_repeater10.key}, lastname{var:area_repeater10.key}, phone{var:area_repeater10.key}, I need to create columns for firstname1, lastname1, phone1, firstname2, etc?

Thanks!
NickOg 14 Jan, 2018
I am not sure if this will help but I use a repeater to save a variable length list of dates. I use these functions

  function packDates($formData) {
    $datesPacked = NULL;
    foreach ($formData as $key => $value) {
      if (substr($key, 0, 11) == 'nmClassDate') {
        $datesPacked .= substr($value, 0, 10) . '|';
      }
    }
    return substr($datesPacked, 0, -1);
  }
function packAndCleanUp(& $thisData) {
  $packedDates = packDates($thisData);
  $thisData['packedDates'] = $packedDates;
  return NULL;
}


and then save the packed set of dates into a single field 'packedDates'
You would need the reverse of course
  function unpackDates($packedDates) {
    /** explode the incoming string into an array by | separator  * */
    $unpackDatesScratch = explode('|', $packedDates);
    $unpackDates = array();
    /** convert each date item to dd-mm-yyy form for the date picker * */
    foreach ($unpackDatesScratch as $aDate) {
      $unpackDates[] = date('d-m-Y', strtotime($aDate));
    }
    /** return array to the form * */
    return $unpackDates;
  }


The principal should work for any other data I think.
Regards

Nick
GreyHead 15 Jan, 2018
Hi farscape,

The most common way to save repeated data from form is to use a second table linked to the first by the new record id.

You can use multiple columns but they is harder to read and re-use; or you can json-encode the data and save it to a single text column - that's simple to do but not so easy to search later.

Bob
farscape 16 Jan, 2018
Thanks NickOg and Bob. I'm afraid that I'm not up to the skill levels that you are and am up against a deadline, so I am having to compromise this time around and limit the output to an email, but that's leading me to another question that I hope is within my abilities. I'm posting it separately because while related, it's different.

Thanks again.
GreyHead 16 Jan, 2018
Hi farscape,

Using a second table does take a little work to create the table but there is no coding or special knowledge required.

Bob
farscape 16 Jan, 2018
I don't mind the work. So does CF6 handles the joins automatically? Is there any documentation on how you can set CF6 to write the repeater area fields to the second table?
farscape 20 Jan, 2018
Bob, Sorry to be such a dolt, but how do you capture the new record ID? I can't find anything in the manual or the forum.
farscape 21 Jan, 2018
OK, I finally have a simple form that saves the repeating rows - sort of. I adapted the simple contact form to capture multiple emails using a repeater area, and then . It saves to the database a row for each email I create, but the value for email in every row of the table is always the first value I entered. I tried to mimic the loop event example in the manual exactly, but obviously I am missing something. I've attached the form and my debugger shows what should be expected.

Array
(
    [option] => com_chronoforms6
    [cont] => manager
    [chronoform] => repeater
    [event] => submit
    [Modela] => Array
        (
            [0] => Array
                (
                    [email] => a@b.com
                )

            [1] => Array
                (
                    [email] => b@c.com
                )

            [2] => Array
                (
                    [email] => c@d.com
                )

        )

    [button7] => 
    [_ga] => GA1.2.226007831.1505575861
    [_gid] => GA1.2.2069064078.1516157373
    [joomla_user_state] => logged_in
    [e9500eaa5818cf303cc3488675b7798a] => lDntIMFtuX5jQLz0aSgUr1
    [b7641e2ed7efee042adee21593901dd3] => Qe-RKRyUTZhDZz,WLAaJ-0
    [__atuvc] => 0|48,5|49,13|50,1|51,10|3
    [d3afdd8bd0d018da056b0d2c45021e09] => UyR9eO,ijStuUweIThbcD3
    [ec3e5ab2c77c5e545fc9f1f89eee2121] => EqmKcLvACIV,l5xfAE8K40
farscape 30 Jan, 2018
Max provided the solutions in a separate thread: In the Save Data action that is inside the Loop Event action, turn off "Auto Save Fields". Thanks Max!
farscape 30 Jan, 2018
Answer
Max provided the solutions in a separate thread: In the Save Data action that is inside the Loop Event action, turn off "Auto Save Fields". Thanks Max!
This topic is locked and no more replies can be posted.