dongzha3058 2015-11-13 14:27
浏览 13
已采纳

在Codeigniter中刷新具有不同数据值的视图

Initially when my view loads, it should display the first question and answer. When press the next button I need to pass next value to the model and get the second question and answer, and if next button is clicked again it should display the third question and answer. The next button should be clickable until the last question appears. I have the following code:

Model

function getNextQuestions($quizID,$qusNo) {

        $check = "SELECT * FROM question WHERE quiz_id = '$quizID' AND id='$qusNo'";
        $query = $this->db->query($check);

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return NULL;
        }
    }

Controller

public function index()
    {
        $this->load->model('Model_questions');

        $quizId = $_POST['quizID_click'];

        $data['questions'] = $this->Model_questions->getNextQuestions($quizId,"1");

        $this->load->view('question_view', $data);
}

public function displayNextQuestions()
    {
        $this->load->model('Model_questions');

        $qusId = $_POST['question_nextClick'];

        $data['questions'] = $this->Model_questions->getNextQuestions($quizId,$qusId);

        $this->load->view('question_view', $data);
}

View

<div id="body">

        <?php


            echo $questions[0]->question . "<br>" 
                    . $questions[0]->choice1 . "<br>" 
                    . $questions[0]->answer. "<br>";
                echo '<form action="/Test/index.php/question_controller/displayNextQuestions" method="POST">                
                        <input type="submit" value="Next">
                        <input type="hidden" name="question_nextClick" value="2" />
                    </form>';

        ?>

I didn't get the output that i need, what are the modifications that i should do. Please help me.

  • 写回答

1条回答 默认 最新

  • dongshi1868 2015-11-13 14:59
    关注

    The Model looks good. Revised answer to revised question.

    Note that I have included the whole controller this time and load the database in the constructor instead of in each function. You alway use the model so take care of it with one line of code.

    Controller

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Quest_controller extends CI_Controller
    {
      function __construct()
      {
        parent::__construct();
        $this->load->database();
        $this->load->model('Model_questions');
      }
    
      public function index()
      {
        /* 
        Rather than declare and initialize a bunch of different variables 
        only to reassign them to a structure passed to the view we use 
        that structure (`$data`) all the way through. 
        */
    
        $data["quizId"] = $_POST['quizID_click'];
        //since this is the first time here we set question_num = 1
        $data['question_num'] = 1;
        $data['questions'] = $this->model_questions->getNextQuestions($data["quizId"],
          "1");
        $this->load->view('question_view', $data);
      }
    
      public function displayNextQ()
      {
        $data['quizId'] = $_POST['quizId'];
    
        //Notice how we increment the question number.
        //The new number gets sent to the new view
        $data['question_num'] = $_POST['question_num'] + 1;
    
        $data['questions'] = $this->model_questions
          ->getNextQuestions($data['quizId'], $data['question_num']);
    
        //if the model returns null then the quiz is done
        if(!isset($data['questions']))
        {
          $this->load->view('test_done_view');
        }
        else
        {
          $this->load->view('question_view', $data);
        }
      }
    }
    

    View

    Notice how we set the values of the hidden inputs. Those values will be returned in the $_POST array for use by displayNextQ() on the next submit.

    In case you don't know, this <?= $quizId; ?> is a shorthand way to write <?php echo $quizId; ?>

    <div id="body">
      <?php
      echo $questions[0]->question."<br>".$questions[0]->answer."<br>";
      ?>
      <form action="/testApp/index.php/quest_controller/displayNextQ" method="POST">                
        <input type="hidden" name="quizId" value="<?= $quizId; ?>" />
        <input type="hidden" name="question_num" value="<?= $question_num; ?>" />
        <input type="submit" value="Next">
      </form>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作