How to handle repeater fields in automation?

Tech Articles | February 2, 2025 | Automation, Blog, Coding, FlowMattic, Fluent Forms, Wordpress

One of my most requested videos in the last few months has been how to handle repeaters/iterators/loops in FlowMattic, and in this article and the accompanying video, I will share some additional details on how to do that with a real-world example.

One of the reasons I haven’t created this video is that I was struggling with a real-world example, but then one fell into my lap just a few hours after it became clear that it would be the winner of my poll within the FlowMattic Facebook group.

What’s the example?

A question was asked of me by email from someone to assist them in creating a custom solution to allow them to a) issue unique tickets and b) issue an email to each delegate and a cover email to the booker for an event they were looking to host.

I knew I would use fluent forms and their repeater input field for this, and of course, my recommendation for automation here would be Flowmattic. They have a module to help deal with such data, the iterator module.

Some Important things to note!

In my video and example, I haven’t used the FlowMattic fluent forms integration, and the reason for this is all around how FlowMattic iterators work. It needs JSON data to work, not the form data the fluent forms integration puts into its flow. So I have used a webhook trigger, and again, please note you don’t want to enable a simple response as you need that JSON data to loop/iterate.

Let’s Start with the form!

This all starts with the form; now, in my example, I am using fluent forms as I have fluent forms. It has a repeater input field, and I know how to use it effectively. Repeater Fields is a pro item, as is webhook sending with fluent forms, I believe, so remember that to follow this tutorial, you will need a pro fluent form. Most form plugins have repeater fields and webhooks, but you must check how they all work, although I expect for almost all of them they will be “pro” or “premium” features.

So moving on to the booking form, it’s actually quite simple. A screengrab is shown below:

As you can see, we have a lead booker, which is often the case for a conference or ticket booking and the names and email addresses of the delegates attending our FlowMattic conference, the highlight fields in purple are our fluent form repeater fields, and you add a new row by pushing the plus button the arrow is pointing too.

So this is the basis of our data, and what will get sent to Flowmattic on submission. This is set to trigger our flowmattic webhook, and an example of that data is shown below

Please note our repeater fields are separated and repeating as they should be. In this example, we have five submissions for tickets to loop through below is an overview of our workflow, which is relatively small but hopefully illustrates how our flow will work

So we start with our webhook, and then we “loop” through our delegates and generate a ticket number for them which is secure and unique we then send the delegate their ticket number to there email address and add their name and ticket number to the table which at the end of our loop will be sent to the primary booker, and then we iterate this through our example.

So, in the example of the webhook data we have above this image, we would generate five tickets and send five emails to those delegates; we then end our iterator.

Please Note: Ending iterators is only necessary if you are doing steps outside of your iterator, so if all you are doing is looping, you don’t need to add an end to the iteration.

It should also be noted that FlowMattic has a native iterator store module for storing data similar to my add ticket details function, but I wanted this as an HTML table, so I have chosen to do it with PHP sessions and adding table rows and resetting the table outside of the flow

So the below image is our looped section before this, and after this is our standard flow but this is our iterator and handles our repeater fields.

We then move on once we have done all our looping, and we move to pull our completed table with what would be our five delegates and their five ticket numbers. we then send an email to the booker which contains this information. See the email snippet below:

Finally we then end our flow by resetting the table ready for the next session run.

In this flow, I have used four PHP snippets/functions, and they are all recreated below for you in the code block listed below

  • Generate a Ticket – this generates our unique ticket number and stores it in the database so we know they are unique and not duplicated. They are hashed for security and uniqueness.
    generateUniqueTicketNumber()
  • Add Ticket Details – This adds a row to our table using the addTableRow() function. This needs a Name and ticket variable
  • Get Order Delegate table – this is how we get our HTML table to input into our outgoing email getTable()
  • Reset table – resetTable() resets the table ready for the next flow run.
<?php 
session_start(); // Start session to store table data

// Function to add a row to the table
function addTableRow($name, $ticket) {
    if (!isset($_SESSION['table_rows'])) {
        $_SESSION['table_rows'] = [];
    }

    // Store row as an array
    $_SESSION['table_rows'][] = [
        'name' => htmlspecialchars($name),
        'ticket' => htmlspecialchars($ticket),
    ];
}

// Function to generate the HTML table as a string (returns table)
function getTable() {
    if (empty($_SESSION['table_rows'])) {
        return '<p>No entries available.</p>';
    }

    $html = '<table border="1" cellpadding="5" cellspacing="0">';
    $html .= '<tr><th>Delegate</th><th>Ticket</th></tr>';

    foreach ($_SESSION['table_rows'] as $row) {
        $html .= '<tr><td>' . $row['name'] . '</td><td>' . $row['ticket'] . '</td></tr>';
    }

    $html .= '</table>';

    return $html; // Returns the table as a string
}

// Function to reset/clear the table
function resetTable() {
    unset($_SESSION['table_rows']);
}

function generateUniqueTicketNumber() {
    global $wpdb; // Assuming this runs within a WordPress environment

    $table_name = $wpdb->prefix . 'event_tickets'; // Adjust the table name accordingly

    do {
        // Generate a secure UUID
        $uuid = bin2hex(random_bytes(16)); 
        // Shorten the ticket number using SHA-256 and truncate
        $ticketNumber = strtoupper(substr(hash('sha256', $uuid), 0, 12));

        // Check if the ticket number already exists
        $exists = $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM $table_name WHERE ticket_number = %s",
            $ticketNumber
        ));

    } while ($exists > 0); // Ensure uniqueness

    // Store the ticket number in the database
    $wpdb->insert($table_name, ['ticket_number' => $ticketNumber, 'issued_at' => current_time('mysql')]);

    return $ticketNumber;
}
PHP

Support the Author

buy me a coffee
Really Useful Plugin Logo
Appoligies for any spelling and grammer issue. As a dyslexic i need to rely on tools for this they like me are not perfect but I do try my best