Replace array value in group
- This topic has 9 replies, 3 voices, and was last updated 3 years, 8 months ago by
hannuh.
-
AuthorPosts
-
April 27, 2021 at 6:37 PM #27700
toni bird
ParticipantHi guys I was wondering if anyone could help me with this: I need to update a value of an array in my database .
Value is stored as :a:1:{
i:0;a:9:{
s:15:"setor_atividade";
s:13:"MOBILIZAÇÃO";
s:14:"nome_atividade";
s:20:"Armação Fundação";
s:16:"atividade_status";
s:8:"INICIADA";
s:11:"data_inicio";
s:10:"20/02/2021";
s:8:"data_fim";
s:1:"-";
s:13:"data_previsao";
s:1:"-";
s:10:"data_pause";
s:1:"-";
s:9:"data_play";
s:1:"-";
s:14:"desc_atividade";
s:1:"-";}and I need to swap the value from "INICIADA" to "PARALISADA" using php , is that possible?
thanks a lot
April 28, 2021 at 9:42 AM #27719Long Nguyen
ModeratorHi,
Yes, it is possible. You can use the helper function rwmb_set_meta()
add_action( 'init', function() { $group = rwmb_meta( 'group_id', '', 12345 ); $group['atividade_status'] = 'PARALISADA'; rwmb_set_meta( 12345, 'group_id', $group ); }, 99 );
where
12345
is the post ID. Get more details on this documentation https://docs.metabox.io/rwmb-set-meta/May 3, 2021 at 11:24 PM #27860toni bird
ParticipantHi Long, thanks for your reply, will give it a try.
regards
May 3, 2021 at 11:41 PM #27863toni bird
ParticipantHi Long , does it work for cloned groups as well?
many thanks
May 4, 2021 at 9:20 AM #27874Long Nguyen
ModeratorHi Toni,
The code will work with any fields. For the cloneable group, you need to use a loop to iterate through the group (array) elements.
May 13, 2021 at 2:07 AM #28136toni bird
ParticipantHi Long, many thanks for your reply.
not sure what I am missing. my code is actually printing the below, any help why is not changing the "atividade_status" value , but is adding after the array ?
add_action( 'init', function() { $group = get_post_meta( 15, 'group_atividade', true ); $group['atividade_status'] = 'PARALISADA'; rwmb_set_meta( 'group_atividade',15, $group); foreach ($group as $key=>$val) { if ($key=="atividade_status" && $val=="Iniciada") { $val="Paralisada"; } } }, 99 ); echo '<pre>'; print_r( $group ); echo '</pre>';
and what is printing below.
( [0] => Array ( [setor_atividade_rdo] => teste 01 [nome_atividade_rdo] => Limpeza de canteiro [atividade_status_rdo] => Iniciada [data_inicio_rdo] => 13/05/2021 [data_fim_rdo] => 13/05/2021 [data_previsao_rdo] => 14/05/2021 [data_pause_rdo] => 12/05/2021 [data_play_rdo] => 12/05/2021 [desc_atividade_rdo] => tttt ) [1] => Array ( [setor_atividade_rdo] => rest [nome_atividade_rdo] => Forma de madeira [atividade_status_rdo] => Iniciada [data_inicio_rdo] => 13/05/2021 [data_fim_rdo] => 13/05/2021 [data_previsao_rdo] => 14/05/2021 [data_pause_rdo] => 12/05/2021 [data_play_rdo] => 12/05/2021 [desc_atividade_rdo] => tttt ) [atividade_status_rdo] => PARALISADA
any help would be much appreciated. cheers
May 13, 2021 at 1:10 PM #28146Long Nguyen
ModeratorHi Toni,
If the group is cloneable, you need to set the group value before setting meta. Please try to use this code
add_action( 'init', function() { $groups = rwmb_meta( 'group_atividade', '', 15 ); foreach( $groups as $index => $group ) { if( $index == 0 ) { $groups[$index]['atividade_status_rdo'] = 'PARALISADA1'; } if( $index == 1 ) { $groups[$index]['atividade_status_rdo'] = 'PARALISADA2'; } } rwmb_set_meta( 15, 'group_atividade', $groups ); }, 99 );
May 14, 2021 at 2:45 AM #28170toni bird
ParticipantHi Long, thank you so much, that is perfect
regards.
September 2, 2021 at 6:06 PM #30550hannuh
Participantforeach ( $lomakkeet_template_fields as $single_field ) { $radio_fields = []; foreach ( $radio_fields as $single_radio_field ) { $radio_fields[] = array('field_name' => $single_radio_field); } $form_fields['sub_group_2'] = $radio_fields; } $all_tickets[$key]['sub_group_1'] = $form_fields; rwmb_set_meta( $post_id, 'main_group', $all_tickets );
I have 3 nested groups, and I try to set in rwmb_after_save_post action hook with above (simplified version here) code, but it does not work properly, that $radio_fields part is not working. Should it work in theory like above for nested groups?
September 3, 2021 at 12:54 PM #30569hannuh
ParticipantI got this working already.
-
AuthorPosts
- You must be logged in to reply to this topic.