【问题标题】:Store data obtained using foreach loop from view into the database using CodeIgniter使用 CodeIgniter 将使用 foreach 循环从视图中获得的数据存储到数据库中
【发布时间】:2017-08-24 19:08:34
【问题描述】:

我正在尝试将从表单获得的值存储到数据库中。我被困在我不知道如何从foreach($questions as $row) 单独获取值并将它们存储到数据库中的地方。

我的数据库表有以下列:

variantid | stepid | questionid | newquestion | radiovalues | description

当我点击保存时,我试图将从视图中获得的所有详细信息保存到数据库中。 foreach($questions as $row) 的每个循环获得的每个值都需要存储在新行中。我想我应该使用insert_batch,但我不知道如何将所有数据放入数组中。

我该如何进行呢?以下是我编写的代码。因为我被卡住了,所以我没有在模型中写任何东西。

 <form id="theform">
  <label for="variants">Variants:</label><br>
          <?php
          foreach($variants as $row)
          {?>
               <div class="form-group">

              <input type="radio" name="variants[]" checked="true" id="variants" value="<?php echo $row->variantid ?>"/><?php echo $row->variantname ?><br/>

         <?php }

          ?>


<div id='steplist'>
   <?php

     foreach($substeps as $row){
         ?>
               <div id='img_div'>
                 <input type="radio" name="stepid[]" checked="true" id="stepid<?php echo $row->stepid; ?>" value="<?php echo $row->stepid;     ?>"/><?php echo $row->stepname ?><br/>
</div>
    <?php
  foreach($questions as $each){
  ?><br><br>
  <input type="text" name="questions[]" id="questions<?php echo $each->questionid; ?>" value="<?php echo $each->questionname; ?>" readonly/><br/>
<input type="hidden" name="questionid[]" id="questions<?php echo $each->questionid; ?>" value="<?php echo $each->questionid; ?>"/>

<a id="addq">Add question</a>
<input type="text" class="form-control" name="newquestion[]" style="font-weight:bold;width:100%;height:5%" id="new_questions<?php echo $each->questionid; ?>" /><br>

  <div class="radio" id="answer">
  <label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="no" required>no</label>
  <label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="probably no" required>probably no </label>
  <label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="unknown" required>unknown</label>
  <label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="probably yes" required>probably yes</label>
  <label style="font-weight:bold"><input type="radio" name="radioquestions[<?php echo $each->questionid; ?>][optradio]" value="yes">yes</label>
</div>
<textarea name="question1_text[]" id="description<?php echo $each->questionid; ?>" rows="3" cols="73"></textarea><br>
<?php
}
   ?>
  <?php
  }
  ?>

</div>
</form>
      <input type='button' value='Save' class='save' id='save' />
<script>
    $(document).ready(function(){
   $("#save").click(function(e){
     e.preventDefault();
         $.ajax({
             type:"POST",
             url:"<?= base_url() ?>index.php/task/perform",
             data:$('#theform').serialize(),

               success:function(response){

            alert(response);

           }
         });
   });
 });

这是我的控制器:

public function perform(){
var_dump($_POST);
$variantid = $this->input->post('variants');
$stepid = $this->input->post('stepid');
$questionid = $this->input->post('questions');
$newquestion = $this->input->post('newquestion');
$radioquestions = $this->input->post('radioquestions');
$question1_text = $this->input->post('question1_text');

 $this->load->model('task_model');
 $result= $this->task_model->performdata();
 if($result){
 echo "Data saved successfully";
 }
}

更新

感谢您的快速回复。我根据建议更改了代码。如何将这些值输入控制器并将它们发送到数据库中?

【问题讨论】:

  • 您的 HTML 中不能有多个具有相同 id 的 。您正在 foreach 上创建多个输入,它们都具有相同的 id "variants"
  • 另一个问题,你在另一个里面有一个 foreach(用于问题和子步骤),但是你对它们使用相同的变量($row),因为内部 foreach 使用与外部相同的范围, $row 将被覆盖。
  • 显示您的型号代码$result= $this-&gt;task_model-&gt;performdata();

标签: php mysql codeigniter foreach


【解决方案1】:

foreach 中的 HTML 无效,因为它缺少结束 div 标记,并且您的输入没有正确的 name 属性,对于多个输入,您应该使用 variants[] 而不是相同的 ID。这样做:

<?php foreach ($variants as $row) { ?>
    <div class="form-group">
        <input type="radio" name="variants[]" checked="true" class="variants" value="<?php echo $row->variantid ?>"/><?php echo $row->variantname ?><br/>
    </div>
<?php } ?>

【讨论】:

    猜你喜欢
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2019-06-17
    相关资源
    最近更新 更多