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;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.