1c2w3 Posted February 7, 2013 Share Posted February 7, 2013 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>'; } } Quote Link to comment Share on other sites More sharing options...
requinix Posted February 7, 2013 Share Posted February 7, 2013 Seems alright enough. What does your form look like? And what is the problem you're having? "No joy" isn't really much for us to go on. Quote Link to comment Share on other sites More sharing options...
1c2w3 Posted February 8, 2013 Author Share Posted February 8, 2013 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> Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 8, 2013 Share Posted February 8, 2013 There is no code to even check their answers, let alone count the correct ones. Quote Link to comment Share on other sites More sharing options...
1c2w3 Posted February 8, 2013 Author Share Posted February 8, 2013 Please see first section of code, this is what checks if the answer is correct or not. Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 8, 2013 Share Posted February 8, 2013 So instead of incrementing $count in the loop, just increment it when the answer is correct. Remove $count from your loop structure. Quote Link to comment Share on other sites More sharing options...
1c2w3 Posted February 8, 2013 Author Share Posted February 8, 2013 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! Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 8, 2013 Share Posted February 8, 2013 Well you have $times for that. You have two variables that right now hold the exact same values throughout your entire script. Change one of them to be useful. Quote Link to comment Share on other sites More sharing options...
1c2w3 Posted February 8, 2013 Author Share Posted February 8, 2013 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 8, 2013 Share Posted February 8, 2013 (edited) $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 February 8, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
1c2w3 Posted February 8, 2013 Author Share Posted February 8, 2013 Thank you so much! I had an idea that it would end up being something simple and that I was probably thinking too much in to it. I really can't thank you enough! And thank you for your patience. 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.