0

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

}
1
  • 1
    Check the count of the variable, if it less than 1, then array is empty Commented Jun 9, 2016 at 9:46

2 Answers 2

2

You can check an array like the following with is_array and/or count:

if (is_array($var) === true && count($var) > 0) {
    //here your code
}

//or...

if ((count($var) < 1) === false) {
    //here your code
}

On the first condition: Don't use only count because:

If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned. There is one exception, if array_or_countable is NULL, 0 will be returned. http://php.net/manual/en/function.count.php

Sign up to request clarification or add additional context in comments.

Comments

0

Try This way.

$arr = [ 'key' => NULL ];

var_dump(array_key_exists('key', $arr));
var_dump(isset($arr['key']));

Check :

if(array_key_exists($key, $arra) && is_null($arr[$key]))
{
    echo 'key exists with NULL value';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.