2017-02-09 32 views
0

你好我只是個在笨新手,我只想問如何解決這個問題?如何獲取codeigniter中函數shuffle的確切值?

我只想做一個簡單的測驗系統,我想洗牌從我的數據庫,並顯示所有問題它..問題是當我比較這些問題的選擇,它給了我一個抽空的價值如何解決這個問題?

這是我的控制器,以顯示我的問題

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; 

} 

請幫助謝謝

+0

同時指定'getQuizID()'和'resultsScore(代碼模型 –

+0

的我剛剛更新了其先生,我把內容getQuizID和resultsScore –

回答

0

做出這樣的查詢:)`

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

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