Support

Account

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

Solved

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.

  • My guess is that your script is timing out. Removing and adding 1000+ rows to a repeater meas the you’re likely doing 3000+ queries on the database to update all of the rows.

  • I actually runned it many many times with the same (or very similar) ammount of rows and it takes less than 300 seconds to complete, why the limit is set to 600.

    Also, PHP timeouts use to appear in my logs, but in this case they did not.
    However, this seems to be fixed. I don’t know how, maybe some browser cache related issue, I don’t really know.

    Anyway, I am making this thread as resolved. Thank you.

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

The topic ‘add_row() and delete_row() don't seem to work (sometimes)’ is closed to new replies.