Replace array value in group

Support MB Group Replace array value in groupResolved

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #27700
    toni birdtoni bird
    Participant

    Hi 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

    #27719
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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/

    #27860
    toni birdtoni bird
    Participant

    Hi Long, thanks for your reply, will give it a try.

    regards

    #27863
    toni birdtoni bird
    Participant

    Hi Long , does it work for cloned groups as well?

    many thanks

    #27874
    Long NguyenLong Nguyen
    Moderator

    Hi 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.

    #28136
    toni birdtoni bird
    Participant

    Hi 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

    #28146
    Long NguyenLong Nguyen
    Moderator

    Hi 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 );
    #28170
    toni birdtoni bird
    Participant

    Hi Long, thank you so much, that is perfect

    regards.

    #30550
    hannuhhannuh
    Participant
    foreach ( $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?

    #30569
    hannuhhannuh
    Participant

    I got this working already.

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.