dongwujie7477 2019-03-15 11:11
浏览 12

我使用CodeIgniter进行了测验,想知道如何使用phpmyadmin存储测验结果

I would like to make a results table on phpmyadmin with the following fields: username, score, subject and ranking. I already have a users and quiz table, users table containing the username and quiz table containing the subject. When the quiz is submitted, it displays a score by viewing the users selected answers and then checks in the quiz table if the user selected the correct answer out of a possible 4 options (fields are choice1, choice2, choice3 and answer but it is randomized when outputted). I would like it that when the user completes the quiz, it will store the result including username, subject, score and ranking (for example 10/10 = A, 5/10 = D). Please help!

Results display view:

<div id="container">
    <h1>Play the Computing Quiz!</h1>

    <?php $score =0; ?>

      <?php $array1= array(); ?>
      <?php $array2= array(); ?>    
      <?php $array3= array(); ?>
      <?php $array4= array(); ?>
      <?php $array5= array(); ?>
      <?php $array6= array(); ?>
      <?php $array7= array(); ?>
      <?php $array8= array(); ?>

         <?php foreach($checks as $checkans) { ?>
               <?php array_push($array1, $checkans); } ?>


        <?php foreach($results as $res) { ?>
               <?php array_push($array2, $res->answer); 
                     array_push($array3, $res->quizID); 
                     array_push($array4, $res->question); 
                     array_push($array5, $res->choice1); 
                     array_push($array6, $res->choice2); 
                     array_push($array7, $res->choice3); 
                     array_push($array8, $res->answer); 
               } ?>


           <?php 
               for ($x=0; $x <10; $x++) { ?>

    <form method="post" action="<?php echo base_url();?>index.php/welcome/index">  


    <p><?=$array4[$x]?></p>


      <?php if ($array2[$x]!=$array1[$x]) { ?>

           <p><span style="background-color: #FF9C9E"><?=$array1[$x]?></span></p>
           <p><span style="background-color: #ADFFB4"><?=$array2[$x]?></span></p>

      <?php } else { ?>

           <p><span style="background-color: #ADFFB4"><?=$array1[$x]?></span></p>

           <?php $score = $score + 1; ?>

    <?php } } ?>

    <br><br>

    <p><b>Your Score: </b></p>
      <p><b><?=$score?>/10</b></p>

    <input type="submit" value="Play Again!">

    </form>

</div>

</body>
</html>

Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Questions extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function quizdisplay()
    {

        $this->load->model('quizmodel');
        $this->data['questions'] = $this->quizmodel->getQuestions();
        $this->load->view('play_quiz', $this->data);

    }

        public function resultdisplay()
    {
        $this->data['checks'] = array(
             'ques1' => $this->input->post('quizid1'),
             'ques2' => $this->input->post('quizid2'),
             'ques3' => $this->input->post('quizid3'),
             'ques4' => $this->input->post('quizid4'),
             'ques5' => $this->input->post('quizid5'),
             'ques6' => $this->input->post('quizid6'),
             'ques7' => $this->input->post('quizid7'),
             'ques8' => $this->input->post('quizid8'),
             'ques9' => $this->input->post('quizid9'),
             'ques10' => $this->input->post('quizid10'),
        );

        $this->load->model('quizmodel');
        $this->data['results'] = $this->quizmodel->getQuestions();
        $this->load->view('result_display', $this->data);
    }
}

Model:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class quizmodel extends CI_Model {

    public function getQuestions()
    {
        $this->db->select("quizID, question, choice1, choice2, choice3, answer, subject");
        $this->db->from("quiz");
        $this->db->where("subject",'computing');


        $query = $this->db->get();

        return $query->result();

        $num_data_returned = $query->num_rows;

        if ($num_data_returned < 1) {
          echo "There is no data in the database";
          exit();   
        }
    }
}
  • 写回答

1条回答 默认 最新

  • duanbei7005 2019-03-15 13:17
    关注

    Your appraoch is incorrect. What I am getting from your question is, when a user plays the quiz and submits his/her answer, you will get an array of answers like,

    Step1:

        $quiz=array(
              'answer_1' => 4,
              'answer_2' => 2,
              'answer_3' => 1,
              'answer_4' => 3,
              .....
              .....
              'answer_10' =>2,
    
    )
    

    Step2:

    I guess your 'Quiz table' stores data for correct answer (you have not described it well)

    Hopefully your quiz table stores data like,

    ans_col   - marks_col
    answer_1  -  1
    answer_2  -  4
    answer_3  -  4
    answer_4  -  3
    answer_5  -  2
    answer_6  -  2
    answer_7  -  1
    answer_8  -  4
    answer_9  -  3
    answer_10  - 1
    

    Run Query:

    SELECT question_number,marks 
    FROM Quiz
    

    This will return you an array, say $answer.(check whether $answer array has same structure as that of $quiz array, if not then make structure of $answer same as of $quiz array.)

    Now, Step:3

    $result = array_diff_assoc($quiz,$answer);
    $wrong_ans = count($result);
    $marks_scored = 10 - $wrong_answer;
    if($marks_scored >= 8){
        $grade = 'A';
    }
    elseif($marks_scored > 5 && $marks_scored < 8)
    {
        $grade = 'B';
    }
    elseif($marks_scored > 2 && $marks_scored < 5)
    {
        $grade = 'c';
    }
    else
    {
        $grade = 'd';
    }
    

    step:4 For your Requirements,

    $username = fetch it from user table using user_id;
    $subject = fetch it from user table using subject_id;
    $score = $marks_scored;
    $ranking = $grade;
    

    Step:5

    Now make an array say $output, make keys like username,subject,score,ranking with their respective value and pass this array '$output' to the view file.*

    Note: Check for array structures.

    Hopefully it clears your doubt.

    评论

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果