Support

Account

Home Forums Add-ons Repeater Field add_row() and delete_row() don't seem to work (sometimes)

Helping

add_row() and delete_row() don't seem to work (sometimes)

  • Hi, I’ll try to be brief. I have created a simple import function. Basically what it does:
    1) You upload a .ZIP file
    2) You upload a .CSV file
    3) The script matches the CSV and existing ZIP files records and store them in an array
    4) The script loops through repeater field and delete every row using delete_row()
    5) Lastly, the scripts loops through an array from step 3 and adds each record to the repeater field using add_row()

    I have added logs to every part of the script. The ZIP gets extracted every time successfully and both delete_row() and add_row() functions work in every single record – they do NOT return false, ever.

    The thing is, sometimes they just don’t add. The repeater field is either empty, or only the portion is added. E.g. my log says 1000 records were added (it literally means add_row() was executed successfully 1000 times), but only 150 records are present, and so on.

    No error whatsoever. PHP error logs are empty. I’m adding the samples of code I am using:

    // Delete all repater rows
    $repeater = get_field('my_repeater', 'option');
    if(!empty($repeater)) {
        $count = count($repeater);
        for($i = $count; $i > 0; $i--) {
            if(delete_row('my_repeater', $i, 'option') == false) {
                $delete_error = 1;
            }
        }
        if(!$delete_error) $output .= 'All records successfully deleted';
        else $output .= 'There was an arror during repeater deletion';
    }
    // Add all new records to repeater
    foreach($rows_ as $row) {
        $data = array(
            'number' => $row['number'],
            'name' => $row['name']
        );
        if(add_row('my_repeater', $data, 'option') == false) {
            $add_error = 1;
        }
        $output_count++;
    }
    if(!$add_error) {
        $output .= 'New records successfully added. Count: ' . $output_count;
    } else {
        $output .= 'There was an error during repeater insertion';
    }

    The problems seems random. My client is logged in with the same user, same user role and everything. He clicks “Import”, the repeater gets messed up. I click it and it is fine. Next time it works for him as well.

    What could be the problem?

  • Additional info: The only thing that seems somehow logical to me is, that the execution of the import script (looping and adding rows) might get interrupted if someone displays the page when the repeater is being loaded. But it seems like a long shot and not very probable.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.