简体   繁体   English

如何在Codeigniter PHP MySQL中创建动态数组

[英]how to create dynamic array in codeigniter php mysql

I really need help on this. 我真的需要帮助。 I have an array that takes data from my input radio buttons. 我有一个数组,可从输入单选按钮获取数据。 But the problem is these input buttons may not follow the exact numbers because of my where clause in the select statement. 但是问题在于,由于我的select语句中的where子句,这些输入按钮可能无法跟随确切的数字。

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

<?php foreach($questions as $row) {
    ?>

    <?php $ans_array = array($row->correct, $row->wrong, $row->wrong1, $row->wrong2);
    shuffle($ans_array);
    ?>
<p><?=$row->quiz_question?></p>
<input type="radio" name="quizid<?=$row->id?>" value="<?=$ans_array[0]?>" required/><?=$ans_array[0]?><br/>
<input type="radio" name="quizid<?=$row->id?>" value="<?=$ans_array[1]?>"/><?=$ans_array[1]?><br/>
<input type="radio" name="quizid<?=$row->id?>" value="<?=$ans_array[2]?>"/><?=$ans_array[2]?><br/>
<input type="radio" name="quizid<?=$row->id?>" value="<?=$ans_array[3]?>"/><?=$ans_array[3]?><br/>


<?php
}
?>
<br/>
<input type="submit" value="Finish"/>
</form>


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'),
 'ques11'=> $this->input->post('quizid11'),
  );

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

I want the array to be dynamic. 我希望数组是动态的。 because if the data in the database is more than eleven(11) then the user will not get result for the rest of the quiz. 因为如果数据库中的数据大于十一(11),则用户将无法获得其余测验的结果。

If you change your resultdisplay() function round to first fetch the questions and then look for an answer to each question... 如果将您的resultdisplay()函数更改为首先获取问题,然后寻找每个问题的答案...

public function resultdisplay()
{
    $this->load->model('quizmodel');
    $this->data['results'] = $this->quizmodel->getQuestions();
    $this->data['checks'] = [];
    for ( $i = 1; $i <= count($this->data['results']); $i++ )   {
        $this->data['checks'][] = $this->input->post('quizid'.$i);
    }
    $this->load->view('result_display', $this->data);
}

This will end up with an array (with a numeric index - you can change that by changing $this->data['checks'][$keyName]= with whatever you want as $keyName ). 这将结束与一个阵列(具有数字索引-你可以通过改变改变$this->data['checks'][$keyName]=与任何要用作$keyName )。 Any missing answers will have null , again you can change this if needed at :null; 任何缺失的答案都将为null ,如果需要,您也可以在:null;处更改它:null; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM