tony09uk Posted November 27, 2012 Share Posted November 27, 2012 I am creating a quiz builder as part of an exercise. It allows the user to make a multiple choice quiz. They will be able to add a title, choose the number of questions they want and how many options they want for each question by inputting the values into a html form (all questions will have the same number of options). The code I have made for this is: <form method="post" action="quiz_form.php"> Quiz name<br/><input type="text" name="name" value="" maxlength="" /><br/> Choices<br/><input type="text" name="choices" value="" maxlength="3" /><br/> Questions<br/><input type="text" name="questions" value="" maxlength="3" /><br/> <input type="submit" name="submit" value="SUBMIT" /> </form> The next part of the quiz maker is to generate the html for the quiz and allow the user to input their questions in to that HTML. The code I have made for this is: if (isset($_POST['submit'])) {//form submitted $errors = array(); if (isset($_POST['choices'])) {//Number of choices per question if (is_numeric($_POST['choices'])) {//it is a number if (strlen($_POST['choices']) <= 3) {//there are no more than three charaters $choices = $_POST['choices']; } else {//add to errors array $errors[] = "<b>CHOICES: </b>You may only go upto 999 questions"; } } else {//add to errors array $errors[] = "<b>CHOICES: </b>Must be a number"; } } else {//add to errors array $errors[] = "<b>CHOICES: </b>Select number of choices"; } if (!empty($_POST['name'])) {//quiz name added $quizname = $_POST['name']; } else {//add to errors array $errors[] = "<b>Quiz name: </b>Add quiz name"; } if (isset($_POST['questions'])) {//number of questions specified if (is_numeric($_POST['questions'])) {//it is a number if (strlen($_POST['questions']) <= 3) {//there are no more than three charaters $questions = $_POST['questions']; } else {//add to errors array $errors[] = "<b>QUESTIONS: </b>You may only go upto 999 questions"; } } else {//add to errors array $errors[] = "<b>QUESTIONS: </b>Must be a number"; } } else {//add to errors array $errors[] = "<b>QUESTIONS: </b>Select the number of questions"; } if (empty($errors)) {//display choices if(isset($questions) && isset($choices)){//choices and question number selected echo "<h1>Change the values in each box</h1>\n";//headline echo "Quiz title: " . $quizname; //users quiz title echo "<form method=\"post\" action=\"addToDb.php\">\n";//start form for($q = 1; $q <= $questions; $q++){//loop through number of questions required echo " <input name='questions[]' type='text' value='question $q' /><br/>\n"; for($opt = 1; $opt <= $choices; $opt++){//loop through number of options required echo " <input name='choice[]' type='text' value='option $opt' /><br/>\n"; } } echo " <input name='submit' type='submit' value='SUBMIT' />\n";//submit form echo "</form>";//close form } } else {//echo errors foreach ($errors AS $v) { echo $v . "<br/>"; } } } else {//end $_POST['submit'] echo "fill out form"; } Finally I am trying to show what the user has created (This is where my first problem arises!). The code I have made for this is: if(isset($_POST['questions']) && isset($_POST['choice'])){ $q = $_POST['questions']; $c = $_POST['choice']; foreach($q AS $v){ echo $v . "<br/>\n"; foreach($c AS $value){ echo " " . $value . "<br/>\n"; } } }else{ echo 'not set'; } When I run the above program I get a problem at the final stage. That problem is that the options do not seem to be linked to the questions i.e. What I expect (Assuming 2 questions and 2 options are chosen): question 1 option 1 option 2 question 2 option 1 option 2 What I get (Assuming 2 questions and 2 options are chosen): question 1 option 1 option 2 option 1 option 2 question 2 option 1 option 2 option 1 option 2 a previous answer has suggested using the count() function and I have tried putting that advice into practice, but cant without success. I have considered using sessions, but I am sure that sessions would not be the right way to go. I assume that the loop is not linking the questions and choices array together for some reason and need to work out a way to get the code to link the two in order to get the output I expect. Please can someone tell me what I am missing. PS I know there are security problems at the moment, and will address them once I can complete the basics of this task. Quote Link to comment Share on other sites More sharing options...
printJimy Posted November 28, 2012 Share Posted November 28, 2012 (edited) this is what appears to me , the only thing that I changed is that i add an <br> inside of for loop like this: for($q = 1; $q <= $questions; $q++){//loop through number of questions required echo " <input name='questions[]' type='text' value='question $q' /><br/>\n"; for($opt = 1; $opt <= $choices; $opt++){//loop through number of options required echo " <input name='choice[]' type='text' value='option $opt' /><br/>\n"; } [color=#ff0000] //here [/color] [color=#ff0000] echo "<br>";[/color] } Edited November 28, 2012 by printJimy Quote Link to comment Share on other sites More sharing options...
tony09uk Posted November 28, 2012 Author Share Posted November 28, 2012 Thanks for testing the script. Can you please confirm that there was no issue with the script on your computer? and that this part was used: if(isset($_POST['questions']) && isset($_POST['choice'])){ $q = $_POST['questions']; $c = $_POST['choice']; foreach($q AS $v){ echo $v . "<br/>\n"; foreach($c AS $value){ echo " " . $value . "<br/>\n"; } } }else{ echo 'not set'; } As that is the part that is outputting the repeated code. (The idea of the above is to show the user what they have input and place it into a database). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.