Support

Account

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

  • 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;
    }