I have multiple choice questions and depending on the progressive score, users need to move to different questions. For example, each question has 4 answers, each with a score.
Question 1
Answer 1 = 5 points
Answer 2 = 10 points
Answer 3 = 15 points
Answer 4 = 20 points
Question 2
Answer 1 = 5 points
Answer 2 = 10 points
Answer 3 = 15 points
Answer 4 = 20 points
The conditions are set in the backend where I can say if the total score is 25 then go to question 7 otherwise if any other score other than 25, proceed to the next question ie: question 3.
I am just using anchor tags to move between the questions. Where I get tripped up is if you look in the LOGIC bit below. The jump to logic requires the current score to compare to. To pinpoint the line, it is this one:
<?php if( get_field( 'go_to' ) && get_sub_field( 'score' ) == // CURRENT SCORE): ?>
The current score changes every time a user clicks on a radio button choice.
$().ready(function(){
function calcscore(){
var score = 0;
$(".calc:checked").each(function(){
score+=parseInt($(this).val());
});
}
$(".calc").change(function(){
calcscore()
});
});
PHP:
<?php if ($questions->have_posts() ):
while ($questions->have_posts() ): $questions->the_post();
?>
<!--Show Question-->
<div class="question-container" id="<?php echo get_the_ID(); ?>">
<div class="question">
<h5><?php the_title(); ?></h5>
<!-- Show answers-->
<?php if (have_rows( 'answers' ) ):
while (have_rows( 'answers' ) ): the_row();
?>
<input type="radio" name="input<?php echo get_the_ID(); ?>" id="input<?php echo get_row_index(); ?>" value="<?php echo get_sub_field( 'score' ); ?>" class="calc">
<label for="input<?php echo get_row_index(); ?>"><?php echo get_sub_field( 'answer' ); ?></label>
<?php endwhile; ?>
<?php endif; ?>
<!--logic-->
<?php if (have_rows( 'condition' ) ):
while (have_rows( 'condition' ) ): the_row();
?>
<?php if( get_field( 'go_to' ) && get_sub_field( 'score' ) == // CURRENT SCORE): ?>
<a href="#<?php echo get_field( 'go_to' )->ID; ?>" class="next-btn">Next</a>
<?php else: ?>
<a href="#<?php echo get_the_ID() + 1; ?>" class="next-btn">Next</a>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<!--end logic-->
<!-- end show answers-->
</div>
</div>
<!--End Question-->
<?php endwhile; wp_reset_postdata(); ?>
<?php endif; ?>