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.