0

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?

5
  • what the error you get Commented Apr 9, 2019 at 21:00
  • it only inserts the first $i it gets Commented Apr 9, 2019 at 21:03
  • should I create a new object inside the for loop? Commented Apr 9, 2019 at 21:04
  • @krystal By doing just that. Don't create the object outside of the loop, create it inside. What you create outside of the loop should be an array. Then assign the new object (within the loop) to a new element in the array each time through the loop, and call the function on that new element. Commented Apr 9, 2019 at 21:05
  • Inside loop. Instance of test is only available when $i=0... Commented Apr 9, 2019 at 21:12

3 Answers 3

1

So the problem in your code, is that you only create 1 instance of Test class (at the top). And inside your for loop, you only reference it and call the insert_question method on it. Beware: in this case only $questionQuiz[0] exists, so in the other cases nothing happens (or an error might occur).

Option 1

If you only want to call the method on the Test class, you could call it in your for loop like this:

(new Test())->insert_question( ... etc ... );

Option 2

If you want to store the created class objects, you create a new Test object, call the insert_question method on it and add the object to your array:

$object = new Test();
$object->insert_question( ... etc ...);
$questionQuiz[] = $object;
Sign up to request clarification or add additional context in comments.

Comments

0

You seem to have the logic all laid out, your just not doing what you're saying.

Create an array, and then add a new object to that array each time through the loop.

$questionQuiz[]  = array();
    if(isset($_POST['quiz_name'],$_POST['professor'],$_POST['course'],$_POST['question'],$_POST['points'],$_POST['answer'], $_POST['is_correct'])) {

...

for($i=0; $i<count($answer); $i++) {
    $questionQuiz[$i] = new Test();
    $questionQuiz[$i] -> insert_question($quiz_name,$professor,$course,$question,$points,$answer[$i],$is_correct[$i]);
            }

...

I will note however, that something seems off about having a function called insert_question() where you individually insert single answers options on seemingly multiple-choice questions. I would think you would pass an array of answer options with corresponding correctness.

4 Comments

The logic inside the function is this: if the question already exists in the table, add its options in the answers table. If not, perform the first insertion of the question too before adding the corresponding answers. I thought of it this way because the user (a professor) can add as many answers as he wants for a question when he creates a quiz. When he presses the "add question" (with its answers) button, I call this script to perform the insertion. Is it not ok? Should I do 2 different scripts?
@krystal So I'm not sure that you need $questionQuiz to be an array at all. Can't you just declare $questionQuiz = new Test(); and then within the loop do $questionQuiz->insert_question(...);? I think you've overcomplicated this.
Well I did this at first but it only inserted in the first iteration and then didn't insert anything else
It works only if I declare the object inside the loop
0

You mentioned storing the posts to an object array. would something like this work?

<?php
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($answer_array) && count($answer_array[5]) > 0)
{
    $count = count($answer_array[5]);
    $questionQuiz[]  = new Test();
    for($i=0; $i < $count; $i++)
    {
        //assuming your class is correct and you'll need to have it handle the incoming array how you want it to
        $answer_array = array($quiz_name, $professor, $course, $question, $points, $answer[$i], $is_correct[$i]);
        $questionQuiz[$i] -> insert_question($answer_array);
    }

}
else
{
    echo "param must not be empty";
}
?>

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.