Created
September 25, 2021 12:23
-
-
Save abedcodes/6dbc727ed4faad8f1a573fdff9fffb0c to your computer and use it in GitHub Desktop.
Simple PHP snippet to render Multi-Choice Questions in a webpage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- this is the main snippet --> | |
<form action="./assert.php" method="post"> | |
<?php foreach($MCQs as $MCQ): ?> | |
<p><?php echo $MCQ['stem']; ?></p> | |
<?php foreach($MCQ['choices'] as $choiceIndex => $choice): ?> | |
<label for="q_<?php echo $MCQ['id']; ?>_c_<?php echo ++$choiceIndex; ?>"> | |
<?php echo $choice; ?> | |
</label> | |
<input | |
type="radio" | |
name="answers[<?php echo $MCQ['id'] ; ?>]" | |
value="<?php echo $choiceIndex; ?>" | |
id="q_<?php echo $MCQ['id']; ?>_c_<?php echo $choiceIndex; ?>"> | |
<?php endforeach; ?> | |
<hr> | |
<?php endforeach; ?> | |
<button type="submit">Assert</button> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- a file to test form submission --> | |
<!-- the $answers is ready for being stored in database --> | |
<?php | |
$answers = json_encode($_POST['answers'], JSON_NUMERIC_CHECK); | |
echo $answers; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- the sample data used in place of queried records from database --> | |
<?php | |
$MCQs = [ | |
[ | |
'id' => 1, | |
'stem' => 'what is php?', | |
'choices' => [ | |
'Markup Language', | |
'Scripting Language', | |
'Programming Language', | |
'None' | |
] | |
], | |
[ | |
'id' => 2, | |
'stem' => 'what is html?', | |
'choices' => [ | |
'Markup Language', | |
'Scripting Language', | |
'Programming Language', | |
'None' | |
] | |
], | |
[ | |
'id' => 3, | |
'stem' => 'what is js?', | |
'choices' => [ | |
'Markup Language', | |
'Scripting Language', | |
'Programming Language', | |
'None' | |
] | |
], | |
]; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- a file to assemble things --> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Multi-Choice Questions</title> | |
</head> | |
<body> | |
<?php require_once('./data.php'); ?> | |
<?php require_once('./_render.php'); ?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment