【问题标题】:Storing Value instead of array in Codeigniter在 Codeigniter 中存储值而不是数组
【发布时间】:2017-11-23 03:12:24
【问题描述】:

在数组中存储单选按钮值(临时存储)。创建了 10 组问题,这 10 组每组有 4 个选项。现在我将它存储在数组中以检查检查了多少单选按钮以及有多少是正确的。 现在问题目前有10个问题。随着数据库的增加,将加载更多问题。

那么如何使用另一种方法或任何其他技术来存储和检查它们。

Codeigniter 新手

这是我存储(在控制器中)并将其重定向到模型然后结果视图页面的代码

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->getQueanswer();
    $this->load->view('result_display', $this->data);
}

任何其他方法,因为不可能每次都为每个新问题及其单选按钮编写代码创建数组

谢谢

【问题讨论】:

    标签: php arrays codeigniter radio-button storage


    【解决方案1】:

    动态一用这个我拿了10个为例,你可以动态增加。

    <form action="/index.php/welcome/getResponse" method="POST">
        <?php for($i = 0; $i < 10; $i++){ ?>
        <div>
            <label><?php echo ($i+1)."."; ?>&nbsp;&nbsp;</label>
            <?php for($j = 0; $j < 4; $j++){ ?>
                <?php echo chr(65+$j); ?><input type="radio" name="<?php echo 'n_'.$i; ?>" value="<?php echo chr(65+$j); ?>">&nbsp;&nbsp;
            <?php } ?>
        </div>
        <br/>
        <?php } ?>
        <input type="submit" name="Submit">
    </form>
    

    在控制器中

    public function getResponse(){
        $resp = array();
        for($i = 0; $i<10; $i++)
        {
            array_push($resp,$this->input->post('n_'.$i));  
        }
        print_r($resp);
    }
    

    输出(样本)

    Array ( [0] => A [1] => B [2] => A [3] => B [4] => C [5] => A [6] => B [7] => C [8] => D [9] => B )
    

    【讨论】:

      【解决方案2】:

      我会使用循环。您的单选按钮如下所示:

      <input type="radio" name="question[1]['a']" value="a"> Question 1, option a<br>
      <input type="radio" name="question[1]['b']" value="b"> Question 2, option b<br>
      ...
      <input type="radio" name="question[2]['a']" value="a"> Question 2, option a<br>
      <input type="radio" name="question[2]['b']" value="b"> Question 2, option b<br>
      ...
      

      由于您提到从数据库中提取数据,我假设您有一个循环来打印无线电输入。

      用于打印 HTML 的 FOR 循环看起来像这样:

      for($x = 1; $x <= 10; $x++) { // number of questions
          for($y = "a"; $y <= "e"; $y++) { // number of options for that question
              echo '<input type="radio" name="question['.$x.'][\''.$y.'\']" value="'.$y.'"> Question '.$x.'<br>';
          }
      }
      

      在 PHP 中,循环是这样的:

      foreach($this->input->post('question') as $index=>$resultArr) {
      // if radio is selected, then $resultArr[0] is the letter option selected and will match the
      // $index value
      }
      

      【讨论】:

        猜你喜欢
        • 2018-09-26
        • 2011-09-14
        • 2017-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-03
        相关资源
        最近更新 更多