如何从codeigniter中的函数shuffle中获取精确值?

时间:2017-02-09 06:57:17

标签: php ajax codeigniter bootstrap-modal

您好我只是codeigniter的新手,我只是想问一下如何解决这个问题......

我只是想制作一个简单的测验系统,我想从我的数据库中移出所有问题并显示它...问题是当我比较问题选择它给我一个没有洗牌的价值我怎么能解决这个问题?

这是我的控制器来显示我的问题

public function quiz()
{
    if(isset($_SESSION['username'])) {
        $this->load->model('quizmodel');
        $this->data['questions'] = $this->quizmodel->getQuestions();
        $this->load->view('client/quiz', $this->data);
    }else{
        $this->load->view('home');
    }
}

这是我的quizmodel

中的getQuestions函数
public function getQuestions()
{
    $this->db->select("cropscience_id, question, choice1, choice2, choice3, answer");
    $this->db->from("cropscience");

    $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();   
    }
}

这是我的测验视图

<div class="panel-body">
<form method="post" action="<?php echo base_url();?>index.php/client_controller/resultdisplay">
    <?php shuffle($questions); ?>
    <?php foreach($questions as $row) { ?>
    <?php $ans_array = array($row->choice1, $row->choice2, $row->choice3, $row->answer);
    shuffle($ans_array); ?>
        <div class="alert alert-success">
            <p><?=$row->question?></p>
            <br>
            <div class="radio radio-success radio-inline">
                <input type="radio" name="<?php echo $row->cropscience_id ?>" value="<?=$ans_array[0]?>" required>
                <label for="inlineRadio1"> <?=$ans_array[0]?> </label>
            </div>
            <div class="radio radio-success radio-inline">
                <input type="radio" name="<?php echo $row->cropscience_id ?>" value="<?=$ans_array[1]?>">
                <label for="inlineRadio1"> <?=$ans_array[1]?> </label>
            </div>

            <div class="radio radio-success radio-inline">
                <input type="radio" name="<?php echo $row->cropscience_id ?>" value="<?=$ans_array[2]?>">
                <label for="inlineRadio1"> <?=$ans_array[2]?> </label>
            </div>

            <div class="radio radio-success radio-inline">
                <input type="radio" name="<?php echo $row->cropscience_id ?>" value="<?=$ans_array[3]?>">
                <label for="inlineRadio1"> <?=$ans_array[3]?> </label>
            </div>
        </div>
    <?php } ?>
    <div align="center" >
        <div class="btn btn-primary btn-rounded">
            <i class="fa fa-check"></i><input class="btn btn-primary btn-rounded" type="submit" value="Submit!">
        </div>
    </div>
</form>
</div>

这是我的控制器中的resultdisplay功能

public function resultdisplay()
{
    if(isset($_SESSION['username'])) {

        $this->load->model('quizmodel');
        $qID = $this->quizmodel->getQuizID();


        $this->data['checks'] = $this->input->post($qID);


        $this->load->model('quizmodel');
        $this->data['results'] = $this->quizmodel->resultsScore();
        $this->load->view('client/result_display', $this->data);

    }else{
        $this->load->view('home');
    }

}

这是我的result_display视图

div class="wrapper wrapper-content">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-primary">
<div class="panel-heading">
    <h1 style="color: white;">Results</h1>
</div>
<div class="panel-body">
<?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 $count = 0; ?>
<?php foreach($checks as $checkans) { ?>
    <?php echo $checkans; ?>
    <?php $array1[$count] = $checkans;
           $count++; ?>
<?php }?>
<br><br>

<?php foreach($results as $res) { ?>
       <?php $array2[] = $res->answer; 
             $array3[] = $res->cropscience_id; 
             $array4[] = $res->question; 
             $array5[] = $res->choice1; 
             $array6[] = $res->choice2; 
             $array7[] = $res->choice3; ?>

        <?php } ?>  

        <?php for ($x=0; $x <= $array3[$x]; $x++) { ?>
        <?php echo $array4[$x]; ?>

        <?php if ($array2[$x] != $array1[$x]) { ?>
            <div class="alert alert-danger">
                    <p><i class="fa fa-times"></i></p>
                    <p><span style="background-color: #FF9C9E"><?=$array1[$x]?></span></p>
                    <p><span style="background-color: #ADFFB4"><?=$array2[$x]?></span></p>
               </div>

          <?php } else { ?>
                <div class="alert alert-success">
                    <p><i class="fa fa-check"></i></p>
                    <p><span style="background-color: #ADFFB4"><?=$array1[$x]?></span></p>

               </div>
               <?php $score = $score + 1 ?>

        <?php } ?>

<?php } ?>


    <div align="center">

            <input type="hidden" name="score" value="<?=$score?>">
            <input type="button" class="btn btn-primary" data-toggle="modal" data-target="#scoremodal" value="View Your Score">                                            


        <!-- Score Modal Body -->

        <div class="modal inmodal fade" id="scoremodal" tabindex="-1" role="dialog"  aria-hidden="true">
            <div class="modal-dialog modal-sm">
                <div class="modal-content">
                    <div class="modal-body" align="center">

                        <h2>Your Score is: </h2>
                        <h1><?=$score?>/100</h1>

                    </div>
                    <div class="modal-footer">
                        <?php echo form_open('client_controller/save_score'); ?>
                        <form method="get">
                        <div align="center">
                            <input type="hidden" name="score" value="<?=$score?>">
                            <input type="submit" class="btn btn-primary" value="Ok">
                        </div>
                        </form>
                        <?php   echo form_close(); ?>

                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
</div>
</div>
</div>
</div>

</div>

这是我的getQuizID()函数模型

public function getQuizID()
{
    $this->db->select("cropscience_id");
    $this->db->from("cropscience");

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

}

这是我的resultsScore()函数模型

public function resultsScore()
{
    $this->db->select("cropscience_id, question, choice1, choice2, choice3, answer");
    $this->db->from("cropscience");

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

    return $query->result();

    $num_data_returned = $query->num_rows;

}

请帮助谢谢

1 个答案:

答案 0 :(得分:0)

进行如下查询:

$this->db->order_by('id', 'RANDOM');

$this->db->order_by('rand()');
$query = $this->db->get('cropscience');
return $query->result_array();