I want to insert the details of a quiz in which among a lot of strings (question, professor, course, etc) , I have one array of answers (and its corresponding array of is_correct), so I thought of creating a for loop to insert each answer properly.
The problem is I don't know how to correctly call the questionQuiz object. I noticed if I declare two different objects at the beginning and then do this manually:
$questionQuiz1 -> insert_question($quiz_name,$professor,$course,$question,$points,$answer[0],$is_correct[0]);
$questionQuiz2-> insert_question($quiz_name,$professor,$course,$question,$points,$answer[1],$is_correct[1]);
it works. How should I declare this object as an array and use it in an iteration?
I tried something like this but it isn't correct.
$questionQuiz[] = new Test();
if(isset($_POST['quiz_name'],$_POST['professor'],$_POST['course'],$_POST['question'],$_POST['points'],$_POST['answer'], $_POST['is_correct'])) {
$quiz_name = $_POST['quiz_name'];
$professor = $_POST['professor'];
$course = $_POST['course'];
$question = $_POST['question'];
$points = $_POST['points'];
$answer = $_POST['answer'];
$is_correct = $_POST['is_correct'];
if(!empty($quiz_name) && !empty($professor)&& !empty($course)&& !empty($question)&& !empty($points)&& !empty($answer) && !empty($is_correct)){
for($i=0; $i<count($answer); $i++) {
$questionQuiz[$i] -> insert_question($quiz_name,$professor,$course,$question,$points,$answer[$i],$is_correct[$i]);
}
}else{
echo json_encode("param must not be empty");
}
}
Should I instantiate $questionQuiz[] = new Test(); inside the loop? I tested and it seems to work, is it correct to do it this way?