簡體   English   中英

PHP MVC測驗-檢查答案

[英]php mvc quiz - checking the answers

我正在嘗試使用Codeigniter2.2.4做一個簡單的測驗,但是我在檢查答案時遇到了一些問題。 我具有以下結構:控制器:Guess模型:People視圖: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來獲取問題,並將問題的正確答案與用戶選擇的內容進行比較。(但是就像我在上面說的那樣,它總是錯誤的)。 到家后,我將發布該方法。

您可以在單一模型功能中完成

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM