Support

Account

Home Forums Add-ons Repeater Field Add_row to nested repeater field (child only) Reply To: Add_row to nested repeater field (child only)

  • HI WooHoo,
    Yes. I run function add_row in my repeater_field loop.

    In ma case I have:
    Lesson / main repeater
    – Lesson active / subfield true/false
    – Lesson finished / subfield true/false
    – Notes / nested repeater
    — Note – custom sub field
    — Note date – another custom sub field

    I need to create empty Notes row at first page run for each lesson. This is a part of much complicated script. It`s a little complicated to explain 🙂

    
    if( have_rows('lesson') ): 
    
        while ( have_rows('lesson') ) : the_row(); 
       
            //////////////////////////////////////////////////////
            // Generate empty rows for each lesson at first run
            //////////////////////////////////////////////////////
            
            // here i have date field which is empty at first start 
            // date is generated at the end 
            if (get_field('start_date') == ''){ 
                
                    $row = array(
                    	'lesson_date'	=> 1,
                    	'lesson_finished'	=> 0,
                    );
                    // add_row('lesson', $row); // this wont work 
                    add_row('field_579eeb638c835', $row); // you must take key of custom field "lesson"
                    
            } 
        endwhile;
    endif;
    
    // fill the field with current date
    if (empty(get_field('start_date'))){
        update_field('start_date', date(Ymd));
    }
    

    Then I need to add notes to lesson no2 – I have for exaple 5 lessons – 5 main repeater field row.

    
    //This is a part of ajax call
    $lesson_id = 2; // main repeater field 
    
    if( have_rows('lesson',$post_id) ): 
     
        while ( have_rows('lesson',$post_id) ) : the_row();
              
              if( get_sub_field('notes', $post_id) ){  
                    
                  $notes_row = get_sub_field('notes', $post_id);
    
              } else {
               
               $notes_row =  array();
               
              }
              
                // Check variables for fallbacks
                if (!isset($input_text) || $input_text == "") { break; }
                
                array_push($notes_row, array( 'note' => $input_text , 'note_date' => date('d.m.Y') ));
                $notes_row = array( 'notes' => $notes_row );
                update_row('lesson', $lesson_id , $notes_row, $post_id); 
              
            }
    
        endwhile;
    
    endif;