Skip to content

Instantly share code, notes, and snippets.

@makzan
Created September 1, 2018 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makzan/5f11851c0c3b51a33e167b9ae3d4e0de to your computer and use it in GitHub Desktop.
Save makzan/5f11851c0c3b51a33e167b9ae3d4e0de to your computer and use it in GitHub Desktop.
Example code for PHP class
<?php
// DATA
$questions=[];
$questions[] = [
"q" => "1+2",
"ans" => "3"
];
$questions[] = [
"q" => "3x5",
"ans" => "15"
];
$questions[] = [
"q" => "6/2",
"ans" => "3"
];
$questions[] = [
"q" => "3-5",
"ans" => "-2"
];
// LOGIC
$currentNum = $_POST["num"] ?? 0;
$q = $questions[$currentNum];
if (isset($_POST["ans"])) {
if ($_POST["ans"] == $q["ans"]) {
$currentNum += 1;
$q = $questions[$currentNum];
}
}
?>
<form action="" method="POST">
<h1><?= $q["q"] ?></h1>
Your answer:<br>
<input type="text" name="ans" autofocus>
<input type="hidden" name="num" value="<?= $currentNum ?>">
<input type="submit">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment