Jump to content

Getting the correct variable in a function


Dorothy17

Recommended Posts

Hi,

I have -probably a basic- issue, which I'm not sure how to solve and start to get completely confused. I'm working on a page (using MVC) which displays test questions and answers retrieved from the database. 

 

It is displayed nicely, my function for displaying the individual tests looks like that:

 

$testID = $_GET['testID'];
$testTitle=$_GET['testTitle'];

$questions=$this->testsRepository->getOneTest($testID);

$testRepository = $this->testsRepository;

require_once __DIR__ . '/../templates/testOne.php';
 

and the view for this (testOne.php):

 

<section>
<h1>Test <?= $testID ?> - <?= $testTitle ?></h1>
<form method="post" action="index.php?action=checkTest">
<?php foreach ($questions as $question): ?>

<p> <?= $question['question'] ?></p>

<?php $answers = $testRepository->getAnswersByQuestionID($question['questionID']);?>
<p>
<input type="radio" name="answer[<?php echo $question['questionID']; ?>]" title="answer" value="answer1"> <?= $answers['answer1'] ?>
</p>
<p>
<input type="radio" name="answer[<?php echo $question['questionID']; ?>]" title="answer" value="answer2"> <?= $answers['answer2'] ?>
</p>
<p>
<input type="radio" name="answer[<?php echo $question['questionID']; ?>]" title="answer" value="answer3"> <?= $answers['answer3'] ?>
</p>


<?php endforeach; ?>
<button type="submit">CHECK</button>

</form>
</section>
 

I can display the individual tests (as per the above code) and I can retrieve the data (the answers) chosen by the participants, but I would like to store the testID, and the score which I calculate from the given results in the DB. Therefore, I have a testResult.php page which is displayed by the checkTest function to get the score. The problem is that, I'm not sure what would be the best way to get the correct testID in my checkTest function for inserting the data into the DB.

 

This is my checkTest function:

 

$correctAnswer=0;
$wrongAnswer=0;

$answers=$_POST['answer'];

foreach ($answers as $questionID=>$answer){

$correctAnswerFromDB=$this->testsRepository->getCorrectAnswer($questionID);
if($correctAnswerFromDB==$answer){
$correctAnswer++;
}
else{
$wrongAnswer++;
}
}
 
$success=$this->testsRepository->insertResult($email, $testID, $correctAnswer);

if($success){
$this->indexAction();

} else{
echo 'An error occurred.';
}
 
require_once __DIR__ . '/../templates/testResult.php';
 

 

Could anyone, please, help me with that?

 

Thanks a lot for any help.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.