l want to calculate the total score from a multiple choice and everything is working well but the problem is how can l validate the radio button for multiple choice questions or just check the associative array if it is empty then execute the code below. l have checked the following links here but failing
<?php
if(isset($_POST['btn_sectionA'])){
$id = $_POST['test_id'];
echo "<br>Test ID".$id."<br>";
$sql = "SELECT multiple_choice.mul_question_number, multiple_choice.mul_que_body, multiple_answer.A, multiple_answer.B, multiple_answer.C, multiple_answer.D, multiple_answer.option_Answer\n"
. "FROM multiple_answer\n"
. "JOIN multiple_choice ON multiple_choice.mul_question_number = multiple_answer.question_number\n"
. "WHERE multiple_choice.test_id =".$id." AND multiple_answer.test_id =".$id."";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
echo '<form method="POST" action="../function/sectionA.php">'; //
while($row = mysqli_fetch_assoc($result)) {
echo "Question :".$row["mul_question_number"]." ".$row["mul_que_body"]."<br>";
echo 'A:<input type="radio" name="answer2question['.$row["mul_question_number"].']" value="A"> '.$row["A"].'<br>';
echo 'B:<input type="radio" name="answer2question['.$row["mul_question_number"].']" value="B"> '.$row["B"].'<br>';
echo 'C:<input type="radio" name="answer2question['.$row["mul_question_number"].']" value="C"> '.$row["C"].'<br>';
echo 'D:<input type="radio" name="answer2question['.$row["mul_question_number"].']" value="D"> '.$row["D"].'<br><hr>';
}
}
echo '<input type="hidden" value="'.$id.'" name="test_id"><input type="submit" value="Submit section A" name="btn_sectionA"></form>';
}else{
echo "failed to get questions";
}
?>
The bove code will be posted to this page and some of the code
foreach($_POST['answer2question'] as $question_number =>$given_answer){
echo "number: ".$question_number." answer:".$given_answer."<br>";
$question_number = mysqli_real_escape_string ($conn, $question_number);
$given_answer = mysqli_real_escape_string ($conn, $given_answer);
$sqlquery = "SELECT question_number FROM multiple_answer WHERE question_number = ".$question_number." AND option_Answer ='".$given_answer."' AND test_id = 1";
$query = mysqli_query($conn,$sqlquery);
if( mysqli_num_rows($query)!== 0 ){
$score += 1;
}
}
countof the variable, if it less than 1, then array is empty