douzi6060 2016-01-22 01:08
浏览 37
已采纳

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?

  • 写回答

2条回答 默认 最新

  • dongmei3869 2016-01-22 01:19
    关注

    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
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口