rand()在PHP MVC中执行两次的解决方法

问题描述:

Ok,

I'm trying to make a website with php to ask a series randoms question (one question per page and stops after 10) to my students from a question bank saved in mysql. When website.com/random is visited, it starts the random controller which then connects to the corresponding model that queries the database for a random question. The database has columns: id, question, choice1, achoice, bchoice, cchoice, dchoice, answer, correctcount, wrongcount. After the user clicks a choice, the form is submitted to website.com/random/selected. If the user answers correctly, then 1 is added to the correct count. elseif, 1 is added to the incorrect count. Here's the model. It's long and cryptic so i'll just summarize it. It's syntactically fine.

class Random_Model {
   private $databaseRow;

   public function __construct() {
      //connects to database
      //counts the number of rows and generate a random number between 1 and number of rows
      //queries the database for the random row into and puts it into an associative array
      //$this->databaseRow = the queried array
   }
   public function selected() {
      //calls $this->databaseRow
      //checks to see if the user answered correctly
      //if correct, add one to 'correctcount'
      //if incorrect, add one to 'incorrectcount'
      //header back to website.com/random where the user will answer another question
   }
   public function returnvariable() {
      return $this->databaseRow
}

Here's the problem. Let's say the correct answer for the frist random question is c. Also, the answer for the second random question is c. If the user clicks c for the first answer, it will add 1 to the correctcount for the second random question. Basically, $databaseRow changes to the next random question after the method selected() is called. So when it checks to see if the user answers correctly, it's checking the answer of the first random question to the answer of the second random question. Is there a remedy to this?

The easiest solution without changing your model too much would be to pass the requested id to the model's constructor. If this value is passed, retrieve the requested one, otherwise perform your random selection logic.

class Random_Model
{
    public function __construct($id = null)
    {
        if (null !== $id) {
            // retrieve the specified row
        } else {
            // retrieve random row
        }
    }
}

Assumed you'll have the original question ID with your answer when submitting to website.com/random/selected. So you should be able to update correctly.

...
<input type="checkbox" name="answer" value="1"/>
<input type="hidden" name="questionID" value="10"/>
...

In your controller, you can retrieve the original question and verify and update it.

public function selected() {
  // retrieve the original question
  $oQuestion = $this->getQuestionById($_POST['questionID']);
  if ($oQuestion['answer'] == $_POST['answer']) {
    $this->addOneToCorrect($oQuestion);
  } else {
    $this->addOneToIncorrect($oQuestion);
  }
  // do the rest of your code
}