【问题标题】:php mvc quiz - checking the answersphp mvc 测验 - 检查答案
【发布时间】:2017-06-21 18:24:33
【问题描述】:

我正在尝试使用 Codeigniter2.2.4 做一个简单的测验,但我在检查答案时遇到了一些问题。 我有以下结构: 控制器:猜 型号:人 视图:guessview、结果

在我的人员类中,我有一种方法可以从数据库中获取所有问题并随机获取一个问题。我在检查答案是否正确时遇到问题。它总是返回错误

function getQuestions()
{
    if(!isset($questions))
    {
        $this->db->select("id, name, choice1, choice2, correctAnswer");
    $this->db->from('questions');
    $query = $this->db->get();
    foreach ($query->result() as $row) {
    $questions[] = $row;
    }
    }

    return $questions;
}

function getRandomQuestion()
{
    $max = count($this->getQuestions());
    $randpos = rand(0, $max-1);
    $question = $this->getQuestions()[$randpos];

    return $question;
}

在我的控制器中:

function guess()
{
    $answer = $this->input->get('answers',false);
    //$questionId = $this->input->get('id',false);

        //this will generate random questions.
        $newQuestion = $this->people->getRandomQuestion();
        $this->load->view('guessview',$newQuestion);
        //this is always false
       if($answer == $newQuestion->correctAnswer)
       {
           //do something
       }
}

在我看来,我只有一个表单,一次只显示一个问题。

//更新 使用您提供的功能时,我的控制器和视图中出现了一些错误。我尝试在我的控制器中抓取一个问题

 $question = $this->people->getRandomQuestion();

//这给了我未定义的索引correctAnswer,但它不应该有,因为数据库表具有以下结构:id、name、choice1、choice2、correctAnswer?

if($question['correctAnswer'] == $answer)
{
}

当我想将数据传递给视图时,即

$question = $this->people->getRandomQuestion();
$data['question'] = $question;
$this->load->view('guessview',$data);

在我看来,我尝试这样的表单值:$question['name'], $question['id'] 我得到未定义的索引名称,id 等。

我的来自guessview.php 的表单

<form id= "form1" class="form"  action="<?php echo base_url();?>guess/people" method='POST'>
                <input type=hidden name=id value="<?php echo $question['id'] ?>"> 
                <label><?php echo $question['name'] ?>
                <div class="radio">
                  <label>
                    <input type="radio" id='rad1' name="answers" value="<?php echo $question['choice1'] ?>">
                    <?php echo $question['choice1'] ?>
                  </label>
                </div>
                <div class="radio">
                  <label>
                    <input type="radio" name="answers"  value="<?php echo $question['choice2'] ?>">
                    <?php echo $question['choice2'] ?>
                  </label>
                </div>
                <div class="radio">
                <label>
                <input type="radio" name="answers"   value="<?php echo $question['correctAnswer'] ?>">
                <?php echo $question['correctAnswer'] ?>
                </label>
                <div>
                    <input type=submit name="sub" value="Answer">
                </div>
</form>

我发帖的问题是控制器功能。似乎当我尝试检查用户是否正确回答时总是错误的。就像用户对上一个问题的回答与当前问题的正确答案进行比较一样。

我尝试获取单选按钮和 id 的输入(放在隐藏字段中) 如果这些返回 false,则表示不显示任何问题并显示第一个问题。否则,如果有输入,则根据 id 获取问题并将问题的正确答案与用户选择的内容进行比较。(但就像我上面所说的那样,它总是错误的)。回家后我会把方法贴出来。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    你可以在单个模型函数中做到这一点

    function getRandomQuestion()
    {
        $sql = "SELECT * FROM questions ORDER BY RAND() LIMIT 1 )";
        $query = $this->db->query($sql);
        $result = $query->result_array();
    
        $count = count($result);
    
        if (empty($count) ||$count > 1) {
            echo "Inavliad Question";
        }
        else{
            return $result;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-21
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2018-06-30
      • 2014-06-02
      • 2012-12-02
      相关资源
      最近更新 更多