Jump to content

Getting the correct variable in a function


Go to solution Solved by Jacques1,

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.

Edited by requinix
forget the color coding and use [code] tags
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.