Jump to content

Limiting loops


realjumper

Recommended Posts

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 help

Neil

Sorry I posted this to the wrong forum. Sorry :(
Link to comment
Share on other sites

  • 3 years later...
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]
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.