Dorothy17 Posted April 2, 2017 Share Posted April 2, 2017 (edited) 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 April 2, 2017 by requinix forget the color coding and use [code] tags Quote Link to comment https://forums.phpfreaks.com/topic/303603-getting-the-correct-variable-in-a-function/ Share on other sites More sharing options...
Solution Jacques1 Posted April 2, 2017 Solution Share Posted April 2, 2017 Sounds like you want to put the test ID into a hidden field within the form. Then you'll receive it together with the answers. Quote Link to comment https://forums.phpfreaks.com/topic/303603-getting-the-correct-variable-in-a-function/#findComment-1544957 Share on other sites More sharing options...
Dorothy17 Posted April 2, 2017 Author Share Posted April 2, 2017 Of course, I should have thought about that! That's exactly what I need! Thanks a lot, Jacques1! Quote Link to comment https://forums.phpfreaks.com/topic/303603-getting-the-correct-variable-in-a-function/#findComment-1544958 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.