realjumper Posted March 21, 2006 Share Posted March 21, 2006 Hi,My query selects from a table of say 25 records. It orders by rand() with a limit of 16. Here is my standard loop[code]while ($row1 = mysql_fetch_assoc($result1)) { $id[] = $row1['id']; $sentence[] = $row1['sentence']; $start[] = $row1['start']; $end[] = $row1['end'];} [/code]The idea is that a sentence (in Japanese Kanji) is displayed, and the sentence is actually a question for which there is only one answer. The user gets to type the answer into a text field and submits the form. The database is queried to see whether the question is right or wrong. A running total of right answers and wrong answers is displayed, as well as the percentage of correct answers from the number of attempts.In my temporary table, the question is recorded as is the correct answer. Also recorded is the users answer, no matter whether their answer is right or wrong.Currently, the user answers the question, the correct answer is displayed and their score incremented. Then, after clicking the 'Next' button, another question is displayed and so on. All of the above takes place in the same php page.....as it needs to (I think) to keep the temporary table alive.What I want to do is to limit the number of attempts to (say) 20....doesn't matter that the same question may be asked more than once. After the 20th attempt, I wish to display the 20 questions with their correct answers as well as the users answer. Then they may attempt the wrong answered questions again if they want to. I can see how to do most of that. What I can't see is how to end the 'test' at 20, so that the option of 'Next' no longer appears. Instead the user will see the total results of his test which he can choose to resit the wrong questions etc.So, my question is how do I end the test at (for example) the 20th attempt.....that is the 20th time the loop has executed?Thanks for any helpNeilSorry I posted this to the wrong forum. Sorry :( Quote Link to comment Share on other sites More sharing options...
Simon Mayer Posted August 19, 2009 Share Posted August 19, 2009 You would need to run a count on the number of times the while loop has gone round and break the while statement at the required point.[code]$count = 0; /* count begins as zero */while ($row1 = mysql_fetch_assoc($result1)) { $id[] = $row1['id']; $sentence[] = $row1['sentence']; $start[] = $row1['start']; $end[] = $row1['end']; $count++; /*increment the count by one */ if($count > 19) { break; /* break the while loop */ }} [/code] 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.