Help with repeating fields array

By: Chris Andrews | Asked: 05/18/2022
ForumsCategory: Paid Help WantedHelp with repeating fields array
Chris Andrews asked 3 years ago
I've been trying to figure this out but am in over my head.  What I've got is a repeating field with two fields in it - an upload field, and a text field.  What is in the text field needs to be added to the image meta as the alt text. So far, I have this: 
add_action('frm_after_create_entry', 'add_uploaded_file_alt', 30, 2);
add_action('frm_after_update_entry', 'add_uploaded_file_alt', 10, 2);
function add_uploaded_file_alt( $entry_id, $form_id ) {
if ( $form_id == 3 ) { //3 being create post/front end editor form
$media_ids = array ($_POST['item_meta'][88]); //88 being upload field id
$alt_text = array ($_POST['item_meta'][91]); //91 being alt text field
$img_meta = array_combine($media_ids, $alt_text);
foreach ($img_meta as $id => $alt) {
update_post_meta( $id, '_wp_attachment_image_alt', $alt );
}
}
}
However, I'm not getting any results for $media_ids and $alt_text, from what Victor tells me, I need to add another array to loop through?  The results of print_r($_POST) are below, so you can see what I am working with.  Can you fix my code?  $50 bounty via PayPal? Here's the post array: 
 
Array   (   [frm_action] => update   [form_id] => 3   [frm_hide_fields_3] =>   [form_key] => add-article   [item_meta] => Array   (   [0] =>   [11] => Testing 382   [12] => <!-- wp:shortcode -->   <p>[display-frm-data id=64744]</p>   <!-- /wp:shortcode -->   [13] =>   [14] =>   [23] =>   [22] =>   [16] => 3   [28] => 0   [25] =>   [17] => 1   [84] =>   [15] => draft   [89] => Array   (   [form] => 9   [row_ids] => Array   (   [0] => i448   [1] => i449   [2] => i450   [3] => i452   )       [i448] => Array   (   [0] =>   [88] => Array   (   [0] =>   [1] => 64763   )       [91] => mother's day flowers test1   )       [i449] => Array   (   [0] =>   [88] => Array   (   [0] =>   [1] => 64764   )       [91] => McDonalds Burger test2   )       [i450] => Array   (   [0] =>   [88] => Array   (   [0] =>   [1] => 64765   )       [91] => which iPhones are waterproof? test3    )       [i452] => Array   (   [0] =>   [88] => Array   (   [0] =>   [1] => 64767   )       [91] => test 4 tip it forward   )       )       [32] =>   [80] =>   [29] =>   [36] =>   [63] =>   [90] =>   [key_pointer] => i452   [parent_field] => 89   )       [frm_submit_entry_3] => a327d92cf9   [_wp_http_referer] => /edit/?frm_action=edit&entry=444   [id] => 444   [item_key] => 2f4d0   [frm_state] => LZeN1XPAlwqHMyJlcMUjSWmNTvvIRWXKTfraM2Tq4GA=   [item_name] => Testing 382   [frm_user_id] => 1   [frm_skip_cookie] => 1   )
 
Chris Andrews replied 3 years ago

<p>Sorry for the poor formatting, this forum doesn't always do what I expect, and if I attempt to edit it just gets worse! </p>

Victor Font replied 3 years ago

You can always post your code in pastebin or gist to retain formatting, then port the link here.

That $_POST data is not showing the values from your repeater. Repeater inputs have complex names such has "item_meta[87][0][89]" where the first dimension is the parent form field id, the second is the row id, and the third is the field id on the repeater.

Using the name example above, the array you need to loop through is $_POST['item_meta'][87]. In $_POST, both the parent field is and row numbers are their own arrays also.

Your var_dump() is showing your repeater field values. Look at item_meta[row_ids]. You have 4-rows in the repeater. Each row's values are listed below the [row_ids]. Based on the data displayed, it appears your alt text field id is 91 and your parent field is 89.

You have to loop through the row_ids to get the value of each row to determine the array to parse that has the actual data. Keep in mind, repeaters are actually embedded form. Your repeater form id in the data above is 9.

Maybe this will help understanding how repeaters are constructed and how to access their data. This article is about jQuery, but the same principles apply to PHP.

https://formidable-masterminds.com/repeaters-and-complex-jquery-operations/

Chris Andrews replied 3 years ago

Hi Victor,

I really appreciate you trying to help me with this but I just can't conceptualize it, and I need to get this working so I can move on to publishing using it.

Would you need to log in to my site to take a look? How much would you charge to get what I need done?

Thanks,

Chris

Victor Font replied 3 years ago

Chris, I'm on vacation through the first week in June. To find a resource that can help you with this, please see my Developers Directory at https://formidable-masterminds.com/developers-directory/

Chris Andrews replied 3 years ago

Thank you,

Chris

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right