Jump to content

First Program With No Tutorial


tony09uk

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/271269-first-program-with-no-tutorial/
Share on other sites

this is what appears to me

post-133998-0-91363400-1354114611_thumb.jpg

 

, 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]
			}

post-133998-0-91363400-1354114611_thumb.jpg

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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.