J. Doe J. Doe - 2 months ago 9
PHP Question

How Can I generate radio buttons based on the number of quiz questions I have?

I'm just starting out with php and wanted to do a fun project to get better with it. I created a text file that starts with the question, then has the answer choices, and then has the answer index:

What does charmander evolve to?#Charmeleon:charizard:squirtle#0
Who is the main character in Pokemon?#Misty:Ash:Brock#1


How can I generate radio buttons based on the number of questions there are?

<?php
$quizStuff = file("quiz1.txt");

foreach ($quizStuff as $questions) {
$questionParse = explode("#", $question);
$answerChoices = explode(":",$questionParse[1]);
echo "$questionParse[0] ? <br />";


foreach ($answerChoices as $answerChoice) {
# create radio button and print answer choice next to it
}
}
?>


Since radio buttons are a type of form, I was thinking that I would have a submit button at the end of the question and the next question would pop up after a user pressed submit. General design input would be great too!

Answer

I am not used to making text file as a database but here is what i see a solution. I put a counter to identify which question is where the choices are included.

$ctr = 1;
    foreach ($quizStuff as $questions) {
         $questionParse = explode("#", $question);
         $answerChoices = explode(":",$questionParse[1]);
         echo "$questionParse[0] ? <br />";


         foreach ($answerChoices as $answerChoice) {
             echo "<input type='radio' id='".$answerChoice.$ctr."' name='question$ctr' value='$answerChoice'> <label for='".$answerChoice.$ctr.">".$answerChoice."</label><br>"
         }
    }