Support

Account

Home Forums Front-end Issues adding/deleting subrows from the frontend returns false

Helping

adding/deleting subrows from the frontend returns false

  • Hi,

    I’m trying to add/delete a row in the backend from the frontend but doesn’t seem to work. Any pointers would be more than welcome.

    I have two nested repeater fields, which I’m adding to a user role:

    + attending courses (repeater)
    + course (relationship)
    + unlocked chapters (repeater)
    + id (text)
    + title (text)

    I’m calling those fields in the frontend and get results as expected, but when calling the “delete_sub_row” or “add_sub_row” functions within the loop, I get a returning false.
    Basically I’m looping through the repeater fields “attending courses” and “unlocked chapters” and if the “$chapterID” is the ID of an unlocked chapter, I want to delete its row.

    Tried different methods without any luck. Any clues?

    Here’s the code:

    $chapterID = 0;
    $currentUser = 66;
    if ( have_rows('attending_courses', 'user_'.$currentUser) ) : 
    while( have_rows('attending_courses', 'user_'.$currentUser) ) : the_row();	
    
    if ( have_rows('unlocked_chapters', 'user_'.$currentUser) ) : 
    while( have_rows('unlocked_chapters', 'user_'.$currentUser) ) : the_row();
    
    if (get_sub_field( 'id' ) == $chapterID) {
    // DELETE THIS ROW	
    $i = delete_sub_row('field_59c8c9d56fa8f', 1, 'user_'.$currentUser);	
    var_dump($i);	
    } else {
    // ADD ROW
    $row = array('id' => $chapterID,'title' => 'foo');
    $j = add_sub_row('unlocked_chapters', $row, 'user_'.$currentUser);
    var_dump($j);	
    }
    endwhile;
    else : 
    // THERE ARE NO ROWS; ADD FIRST ROW
    endif;
    
    endwhile;
    endif;

    —- Versions —-
    ACF v5.6.1
    WP v4.8.2

  • fixed it 🙂
    delete_sub_row and add_sub_row should be out side the second loop

    function chapterdone() {    
    	$chapterID = $_POST['chapterID'];
    	$postID = intval($_POST['postID']);
    	$chaptertitle = $_POST['chaptertitle'];
    	
    	$currentUser = get_current_user_id();
    	
    	$addRowVal = [];	
    	$unlockedChapters->id = [];
    	$unlockedChapters->index = [];						
    	
    
    	if ( have_rows('attending_courses', 'user_'.$currentUser) ) : 
    		while( have_rows('attending_courses', 'user_'.$currentUser) ) : the_row();
    
    // MAKE SURE THE USER IS ATTENDING THE COURSE
    			if (get_sub_field( 'course' )[0] != $postID) {
    				echo "course not registered";
    				wp_die();
    			} else {	
    // DECIDE IF WE DELETE OR ADD A ROW
    				if ( have_rows('unlocked_chapters', 'user_'.$currentUser) ) : 
    					while( have_rows('unlocked_chapters', 'user_'.$currentUser) ) : the_row();;
    
    						if (get_sub_field( 'id' ) == $chapterID) {
    							// (UNLOCK CHAPTER) CHAPTER IS ALREADY UNLOCKED, LOCK IT						
    							array_push($unlockedChapters->id, get_sub_field( 'id' ));
    							array_push($unlockedChapters->index, get_row_index());
    						} else {
    							// (LOCK CHAPTER) CHAPTER IS NOT IN THE LIST, ADD IT 
    							$addRowVal = array('id' => $chapterID,'title' => $chaptertitle);				
    						}
    					endwhile;
    				else : 
    					// (LOCK CHAPTER) THE LIST HAS NO CHAPTERS, ADD THIS ONE
    					$addRowVal = array('id' => $chapterID,'title' => $chaptertitle);
    				endif;
    				
    // ADD OR DELETE THE ROW BASED ON PREVIOUS STEP				
    				if (in_array($chapterID, $unlockedChapters->id)) {
    					//echo "CLICKED CHAPTER " . $chapterID . " IS UNLOCKED. DELETE IT'S ROW: " . $unlockedChapters->index[0] . "------";
    					delete_sub_row('unlocked_chapters', $unlockedChapters->index[0]);
    				} else {
    					//echo "CLICKED CHAPTER " . $chapterID . " IS LOCKED " . print_r($addRowVal);
    					add_sub_row('unlocked_chapters', $addRowVal);
    				}	
    			}
    		endwhile;
    	else: 
    		echo "user is allowed to view the course, but the course is not registered in her profile";
    	endif;
    }
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘adding/deleting subrows from the frontend returns false’ is closed to new replies.