Jump to content

Count from a for statement


1c2w3

Recommended Posts

Hello,

I am new to the site and pretty new to PHP, I decided to set my first challenge creating a times tables script and basically I have made a questions page were the user inputs the answer, when it posts to the next page the code checks to see if the answer is correct or not. What I want to do is at the bottom of the code after displaying each question and answer and if it is correct or not I want to say "you scored 10 out of 12" for example, I get my list of questions and answers from a for statement, could someone please help, I have spent ages on it trying to get the results in an array etc but no joy!

 

Here is my code;

 

 

$times = 1;
$numbers = htmlspecialchars($_POST["postnumber"]);
$count = 1;

for ($times, $count; $times <=12, $count <=12; $times++, $count++)
{
 $result = $times*$numbers;
 if ($_POST["answer" . $count] == $result) {
 echo '<li>';
 echo ($times . " X " . $numbers . " = " . $_POST["answer" . $count] . " " . '<strong>Correct! Well done!</strong>' . '<br/>');
 echo '</li>';
 } else {
 echo '<li>';
 echo ($times . " X " . $numbers . " = " . $_POST["answer" . $count] . " " . '<strong>Incorrect! Try again!</strong>' . '<br/>');
 echo '</li>';


 }


}

Link to comment
Share on other sites

Hi I am trying to make a count so i can show at the bottom how many were answered correctly, i.e. "You scored 9 out of 15".

 

Here is the form code.

 

<form action="testanswers.php" method="post" name="form2" />
 <?php
                   if (isset($_POST['submit']))
                   {
                   echo ('<ul>');    
                       $times = 1;
                       $number = htmlspecialchars($_POST["number"]);
                       $count = 1;

                       for ($times, $count; $times <=12, $count <=12; $times++, $count++)
                           {
                               $counter = htmlspecialchars('answer' . $count);
                               $ans = '';
	 if (isset($_POST['check'])) 
	 {
	  $ans = $counter;
	 } 
	 else 
	 {
	  $ans = '';
	 }

    echo ('<label for="' . $counter . '">' . $times . " X " . $number . " = " .  '</label>');
    echo ('<input type="number" required name="' . $counter . '" value="' . $ans . '" />');
    echo ('</li>');
   } 

 echo ('</ul>');
 echo '<input type="hidden" value="' . $number . '" name="postnumber"/>';
 echo '<input type="submit" value="Check Results" name="check" />';
             }
             ?>
           </form>

Link to comment
Share on other sites

I need $count to loop though so i can display the correct answer, are you saying do another for loop in my if statement (if the answer is correct)? I have just tried lots of different ways, I managed to get a "1" under each correct answer but still can't get it to count this and echo it out at the bottom.

 

Sorry if this is obvious to you!

Link to comment
Share on other sites

Thanks for that now that you pointed that out I am now just using $times for both occasions. But I am really sorry but I just don't know what to do next I am sure it is easy when you know how! I have tried doing another for loop, if statements, counts! I just cant seem to get it! Any chance of a pointer as to what to do next? Thank you for your patience but things are only easy when you know how to do it.

Link to comment
Share on other sites

$times = 1;
$numbers = htmlspecialchars($_POST["postnumber"]);
$count = 1;

for ($times =1; $times <=12; $times++)
{
    $result = $times*$numbers;
    if ($_POST["answer" . $times] == $result) {
    echo '<li>';
$count++;
    echo ($times . " X " . $numbers . " = " . $_POST["answer" . $times] . " " . '<strong>Correct! Well done!</strong>' . '<br/>');
    echo '</li>';
    } else {
    echo '<li>';
    echo ($times . " X " . $numbers . " = " . $_POST["answer" . $times] . " " . '<strong>Incorrect! Try again!</strong>' . '<br/>');
    echo '</li>';


    }


}

 

Now $count holds the number of correct ones.

 

Change the variables names to be more descriptive.

Edited by Jessica
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.