Issue
I am creating a quiz web app using Open trivia DB API and PHP. The issue I'm, having now is checking whether the option a user selected is the correct answer. I realized that the program is making another API call and getting a different set of questions entirely everytime so the options being checked with the selected options are totally different. Is there I can store the first result of the API and then check the selected with it instead of making a new call every time?
This is my Controller
public function callAPI($method, $url, $data){
$curl = curl_init();
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'APIKEY:xxxxxx',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
return $result;
}
public function quiz()
{
$get_data= $this->callAPI('GET', 'https://opentdb.com/api.php?amount=10&category=18', false);
$data = json_decode($get_data, true);
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$sum = 0;
foreach ($data as $key => $value)
{
foreach ($value as $key => $k)
{
if($_POST[$key]== $k['correct_answer'])
{
$sum++;
}
echo"The question is ".$k['question']."you answered $_POST[$key] and the correct answer is ". $k['correct_answer']. " Your score is $sum"; // I used this line to find out that the problem.
}
}
}
else
{
$this->view('quiz', $data);
}
}
This is my view
<div class="col-md-6 grid-margin stretch-card ">
<form class="my-auto" id ="regForm" method="POST" action="">
<div class="card">
<div class="card-body">
<?
foreach ($data as $key => $value):
foreach ($value as $key => $k):?>
<div class="tab">
<span><p><? echo $key+1 ?></p></span>
<h4 class="card-title"> <? echo $k["question"] . "<br>";?></h4>
<? foreach ( $k["incorrect_answers"] as $incorrect):
?>
<div class="form-check">
<label class="form-check-label" for="radio1"></label>
<input type="radio" class="form-check-input" id="radio1" name="<? echo $key ?>" value="<? echo " $incorrect"; ?>"> <? echo " $incorrect"; ?>
</div>
<?endforeach ?>
<input type="radio" class="form-check-input" id="rad2" name="<? echo "$key" ?>" value="<? echo $k["correct_answer"]; ?>"> <? echo $k["correct_answer"]; ?>
<div style="overflow:auto;">
<div style="float:right;">
<button type="button" class="btn btn-outline-danger btn-fw" id="prevBtn" onclick="nextPrev(-1)">Previous</button>
<button type="button"class="btn btn-outline-primary btn-fw" id="nextBtn" onclick="nextPrev(1)">Next</button>
</div>
</div>
</div>
<?endforeach ?>
<?endforeach ?>
</div>
Any help I can get will be much appreciated. Thanks in advance.
Solution
Okay, so you are doing an API request, render a page with questions and then when the user submits the answers want to re-use the result from the original API confirm that the answer is correct?
As the api does not allow you to retrieve the same questions using a request hash or question ID's, you will need to introduce a layer into your code that stores the information from the API request for use in the next request.
The easiest solution, looking at your coding level, would be to start using the $_SESSION global to store the array for questions into the session, then when the user submits their answers check for this session information.
Although I think you should have enough to look at documentation and move further, let's tear this down a little bit:
public function callAPI($method, $url, $data){
$curl = curl_init();
switch ($method){
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'APIKEY:xxxxxx',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
$_SESSION['quizQuestions'] = $result;
return $result;
}
public function quiz()
{
if($_SERVER["REQUEST_METHOD"]=="POST" && isset($_SESSION['quizQuestions']))
{
$sum = 0;
foreach ($_SESSION['quizQuestions'] as $key => $value)
{
foreach ($value as $key => $k)
{
if($_POST[$key]== $k['correct_answer'])
{
$sum++;
}
echo"The question is ".$k['question']."you answered $_POST[$key] and the correct answer is ". $k['correct_answer']. " Your score is $sum"; // I used this line to find out that the problem.
}
}
$_SESSION['quizQuestions'] = null;
// render view..
}
else
{
$get_data= $this->callAPI('GET', 'https://opentdb.com/api.php?amount=10&category=18', false);
$data = json_decode($get_data, true);
$this->view('quiz', $data);
}
}
This obviously needs more work, but I hope you get the gist. Please also notice the fix in $_POST[key]
key would be seen as a constant, not a variable.
Answered By - Luceos Answer Checked By - Willingham (WPSolving Volunteer)