We help IT Professionals succeed at work.
Troubleshooting Question

Saving repeating form input back into individual post_meta arrays

Chris Inman
Chris Inman asked
on
24 Views
Last Modified: 2020-10-24
This page takes names from an ACF repeater field and creates new wp_post_meta entries with arrays as values. So far each single post_meta entry contains an array of the name and a check-in/out time for the days of the week. It then writes these to the database. This is so it can look for new employees that may have been added to the original list.
Now it goes back in an grabs the post_meta entries in a loop by looping the ACF names again to grab each named post_meta entry to display the form.

So far this works.Now once time entries are made in this form for each employee, I need the form to wrap up each row, back into an array of data to be saved back into the post_meta and update that entry with the new times. Subsequent page loads (daily) will reload the post_meta, retaining the previous entries and allowing for more entries.

I know in the end, I will be running an update_post_meta($post_ID, $pay_name, $row_data), I just don’t know how to get each row back into an array of $row_data.

What’s the best way to go about wrapping up that data and updating the post_meta entries?

~Thank you~
Chris

Code So far - I know it’s ugly
<?php
/**
 * The template for displaying all single posts.
 *
 */
if( is_user_logged_in() ){
    global $current_user;
    wp_get_current_user();
    $user_info = get_userdata($current_user->ID);
}

if( is_super_admin() || (is_user_logged_in() && (in_array('operations',$user_info->roles))) ) {

    $url = "//$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    get_header();

    while (have_posts()) : the_post(); //start loop

        $settings_id = get_id_by_slug('settings');
        $post_ID = get_the_ID();

        $post_meta1 = get_post_meta($post_ID); // This is an array that simply holds on to a list of named agents already pulled from settings
        $post_meta2 = json_encode($post_meta1, JSON_UNESCAPED_SLASHES);
//        var_dump($post_meta1);
//        var_dump($post_meta2);
// die;

        if (have_rows('agents_openers', $settings_id)):
            while (have_rows('agents_openers', $settings_id)) : the_row();
// If the exempt field is true then skip, we don't want to add this agent to the timesheet
                if (!get_sub_field('exempt')) {
                    $pay_name = get_sub_field('agent_name');
// If not already set, set default values and create new agent-named array
                    $strpos = strpos($post_meta2, $pay_name);
                    if($strpos !== false) {
                        continue;
                    } else {
                        $settings_names[$pay_name] = array(
                            'name' => $pay_name,
                            'mon_in' => '9:00am',
                            'mon_out' => 0,
                            'tues_in' => 0,
                            'tues_out' => 0,
                            'wed_in' => 0,
                            'wed_out' => 0,
                            'thurs_in' => 0,
                            'thurs_out' => 0,
                            'fri_in' => 0,
                            'fri_out' => 0,
                            'sat_in' => 0,
                            'sat_out' => 0,
                            'sun_in' => 0,
                            'sun_out' => 0,
                            'ot_total' => 0,
                            'reg_total' => 0,
                        );
                    }
                } else {
                    continue;
                }
            endwhile;
        endif;

//die;
    if(isset($settings_names)) {
//        var_dump($settings_names);
        foreach ($settings_names as $names) {
            $name2 = $names['name'];
            $settings = array(
                'pay_name1' => $names['name'],
                'mon_in' => $names['mon_in'],
                'mon_out' => $names['mon_out'],
                'tues_in' => $names['tues_in'],
                'tues_out' => $names['tues_out'],
                'wed_in' => $names['wed_in'],
                'wed_out' => $names['wed_out'],
                'thurs_in' => $names['thurs_in'],
                'thurs_out' => $names['thurs_out'],
                'fri_in' => $names['fri_in'],
                'fri_out' => $names['fri_out'],
                'sat_in' => $names['sat_in'],
                'sat_out' => $names['sat_out'],
                'sun_in' => $names['sun_in'],
                'sun_out' => $names['sun_out'],
                'reg_total' => $names['reg_total'],
                'ot_total' => $names['ot_total']
            );
            update_post_meta($post_ID, $name2, $settings);
        }
    }
//    var_dump($settings_names);
//        die;

//        $post_meta1 = get_post_meta($post_ID); // This is an array that simply holds on to a list of named agents already pulled from settings

        ?>
        <div id="timesheet">
            <div id="content" role="main">
                <p>Timesheet for : --> <?php echo get_the_title(); ?></p>

                <form action="/wp-content/themes/czar/timesheet.php" method="post"  class="sales_form_two" style='display:inline' autocomplete="off">
                    <!--                    <form action="" method="POST"  class="sales_form_two" style='display:inline' autocomplete="off">-->
                    <!--                    <form action="--><?php //echo $url; ?><!--" method="post"  class="sales_form_two" style='display:inline' autocomplete="off">-->
                    <!--                    <form action="--><?php //echo $_SERVER['PHP_SELF']; ?><!--" method="post"  class="sales_form_two" style='display:inline' autocomplete="off">-->
                    <?php
                    $html = '<div class="pay_flex">';
                    $html .= '<span class="pay_week">   Monday  </span><span class="pay_week">  Tuesday  </span><span class="pay_week"> Wednesday </span><span class="pay_week"> Thursday  </span><span class="pay_week">   Friday  </span><span class="pay_week"> Saturday  </span><span class="pay_week">   Sunday  </span></div>';
                    $html .= '<div class="pay_detail">';?>
                    <?php
                    $settings_names1 = array();
                                if( have_rows('agents_openers', $settings_id) ):
                                    while ( have_rows('agents_openers', $settings_id) ) : the_row();
                                        if (!get_sub_field('exempt')) { // If the exempt field is true then skip, we don't want to add this agent to the timesheet

                                            $pay_name = get_sub_field('agent_name');

                                            $settings_names1 = get_post_meta($post_ID, $pay_name, true);
                                            $pay_name1 = $settings_names1['pay_name1'];
                                            $mon_in = $settings_names1['mon_in'];
                                            $mon_out = $settings_names1['mon_out'];
                                            $tues_in = $settings_names1['tues_in'];
                                            $tues_out = $settings_names1['tues_out'];
                                            $wed_in = $settings_names1['wed_in'];
                                            $wed_out = $settings_names1['wed_out'];
                                            $thurs_in = $settings_names1['thurs_in'];
                                            $thurs_out = $settings_names1['thurs_out'];
                                            $fri_in = $settings_names1['fri_in'];
                                            $fri_out = $settings_names1['fri_out'];
                                            $sat_in = $settings_names1['sat_in'];
                                            $sat_out = $settings_names1['sat_out'];
                                            $sun_in = $settings_names1['sun_in'];
                                            $sun_out = $settings_names1['sun_out'];
                                            $reg_total = $settings_names1['reg_total'];
                                            $ot_total = $settings_names1['ot_total'];
// Display data in rows with Entry fields and labels
                                            $html .= '<p><span class="name_space">'.$pay_name1.'</span>' ;
                                            $html .= '<input type="text" class="time_input" name="mon_in" placeholder="IN" value="'. $mon_in .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }">';
                                            $html .= '<input type="text" class="time_input" name="mon_out" placeholder="OUT" value="'. $mon_out .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" style="margin-right: 5px" >';
                                            $html .= '<input type="text" class="time_input" name="tues_in" placeholder="IN" value="'. $tues_in .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" >';
                                            $html .= '<input type="text" class="time_input" name="tues_out" placeholder="OUT" value="'. $tues_out .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" style="margin-right: 5px" >';
                                            $html .= '<input type="text" class="time_input" name="wed_in" placeholder="IN" value="'. $wed_in .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" >';
                                            $html .= '<input type="text" class="time_input" name="wed_out" placeholder="OUT" value="'. $wed_out .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" style="margin-right: 5px" >';
                                            $html .= '<input type="text" class="time_input" name="thurs_in" placeholder="IN" value="'. $thurs_in .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" >';
                                            $html .= '<input type="text" class="time_input" name="thurs_out" placeholder="OUT" value="'. $thurs_out .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" style="margin-right: 5px" >';
                                            $html .= '<input type="text" class="time_input" name="fri_in" placeholder="IN" value="'. $fri_in .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" >';
                                            $html .= '<input type="text" class="time_input" name="fri_out" placeholder="OUT" value="'. $fri_out .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" style="margin-right: 5px" >';
                                            $html .= '<input type="text" class="time_input" name="sat_in" placeholder="IN" value="'. $sat_in .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" >';
                                            $html .= '<input type="text" class="time_input" name="sat_out" placeholder="OUT" value="'. $sat_out .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" style="margin-right: 5px" >';
                                            $html .= '<input type="text" class="time_input" name="sun_in" placeholder="IN" value="'. $sun_in .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" >';
                                            $html .= '<input type="text" class="time_input" name="sun_out" placeholder="OUT" value="'. $sun_out .'" onfocus="if(this.value == \'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'\'; }" ></p>';

                                        }else {
                                            continue;
                                        }
                                    endwhile;
                                endif;


                    $html .='</div>';
                    echo $html; // Collective HTMl is printed to screen

                    //Javascript will create totals and add to arrays total and ot total fields

                    // Run a loop from the above and add the data back into the array with updated time fields
                    //                    Foreach row of user as long as their is length of user rows
                    //                    Write each user row with new time fields back to the array


                    //Submit button will wrap up the arrays of data and save back to post_meta for each name
                    ?>

                    <!--                <div class="change_l" style="float:left;"><a href="#"><input type="submit" value="Submit" id="timesheet_submit" class="chone" style="margin-top:15px;margin-right:10px;"></a></div>-->
                    <div class="change_l" style="float:left;"><a href="#"><input type="submit" value="Submit" class="chone" style="margin-top:15px;margin-right:10px;"></a></div>
                    <?php

                    //            update_post_meta($post_ID, 'settings_names',$settings_names);


                    ?>
                </form>
            </div><!-- #content -->
        </div><!-- #timesheet -->
    <?php
endwhile; // end of the loop
 } elseif( is_user_logged_in() && (!in_array('operations',$user_info->roles)) ){
    echo '<div id="message_alert">You do not have permission to view this page.</div>';
} else{
    echo '<div id="message_alert">You must be logged in to view this page.</div>';
}

get_footer();


The sample array:
Sample Page Output:
Cheers


Comment
Watch Question