【问题标题】:Modify array data in a form within a foreach loop在 foreach 循环中修改表单中的数组数据
【发布时间】:2018-06-27 12:07:08
【问题描述】:

视图页面中有一个将数据作为多维数组的表单。在编辑模式下,表单值通过 foreach 循环获取和显示数据。 现在我的问题是我无法在 foreach 循环中修改该表单数据并将其发送到数据库。我浏览了一些关于 foreach 关键概念的帖子,但是如何在 html 中获取修改后的值并将其发送到这个 foreach 循环中的数据库? 我的代码正确获取数据但无法更新数据。

这是我的查看页面代码:

<table class="table table-striped" id="dataTable">
    <thead>
    <tr>
        <th>Subscription</th>
        <th>Description</th>
        <th>Interval</th>
        <th>Amount</th>
        <th></th>
    </tr>
    </thead>
    <tbody>


    <?php
    if (!empty($invoice_detail)) {
        foreach ($invoice_detail as $key => $result) { ?>
            <tr>
                <td><input type="text" name="invoice[$key][invoice_detail_subs]" id="invoice_detail_subs"
                           value="<?php echo $result->invoice_detail_subs; ?>"/></td>
                <td><input type="text" name="invoice[$key][invoice_detail_desc]" id="invoice_detail_desc"
                           value="<?php echo $result->invoice_detail_desc; ?>"/></td>
                <td><input type="text" name="invoice[$key][invoice_detail_interval]" id="invoice_detail_interval"
                           value="<?php echo $result->invoice_detail_interval; ?>"/></td>
                <td><input type="text" name="invoice[$key][invoice_detail_amount]" id="invoice_detail_amount"
                           value="<?php echo $result->invoice_detail_amount; ?>"/></td>
                <td></td>
            </tr>

        <?php }
    } ?>

    </tbody>
</table>

这是控制器中的更新代码:

if ($this->input->post('Submit')) {
    $query = $this->db->query(
        "SELECT * FROM ".$this->db->dbprefix."invoice_details WHERE invoice_detail_invoie_id ='".$id."' "
    );
    $fetch = $query->row();
    $rows = $query->num_rows();

    if ($id) {


        $data1 = [
            'invoice_detail_subs'     => $this->input->post('invoice_detail_subs'),
            'invoice_detail_desc'     => $this->input->post('invoice_detail_desc'),
            'invoice_detail_interval' => $this->input->post('invoice_detail_interval'),
            'invoice_detail_amount'   => $this->input->post('invoice_detail_amount'),
            'invoice_detail_mddt'     => date("Y-m-d H:i:s")
        ];

        $this->db->update('invoice_details', $data1, 'invoice_detail_invoie_id = '.$id);


    }
}

【问题讨论】:

  • “但是我如何在 html 中获取修改后的值并在这个 foreach 循环中将其发送到数据库?” - 这有什么意义呢?如果您在同一个循环中执行此操作,那么用户将在何时何地如何更改值? (或者,如果您不希望用户能够更改值,但它们应该更改为由脚本逻辑决定 - 那么为什么首先输出带有输入字段的表?)
  • 所以,如果我理解正确的话,你想要做的是显示一个带有预定义值的表单,然后在提交时更新数据库中的数据(所以,一个更新表单)。我不知道“invoice[$key][invoice_detail_subs]”的值是什么,但应该是“invoice_detail_subs”,因为PHP使用名称来标识输入字段。
  • 对不起,我不能问清楚.. 是的,这只是普通的添加,编辑表单,带有提交按钮。但我必须以相同的形式添加和编辑。在 EDIT 模式下,我在 foreach 循环中获取数据。现在我要修改多行的值

标签: php arrays codeigniter foreach


【解决方案1】:

查看代码

<table class="table table-striped" id="dataTable">
<thead>
<tr>
    <th>Subscription</th>
    <th>Description</th>
    <th>Interval</th>
    <th>Amount</th>
    <th></th>
</tr>
</thead>
<tbody>


<?php
if (is_object($invoice_detail)) :
    foreach ($invoice_detail as $key => $result) : ?>
        <tr>
            <td><input type="text" name="invoice_detail_subs"
                       value="<?= $result->invoice_detail_subs; ?>"/></td>
            <td><input type="text" name="invoice_detail_desc"
                       value="<?= $result->invoice_detail_desc; ?>"/></td>
            <td><input type="text" name="invoice_detail_interval"
                       value="<?= $result->invoice_detail_interval; ?>"/></td>
            <td><input type="text" name="invoice_detail_amount"
                       value="<?= $result->invoice_detail_amount; ?>"/></td>
            <td></td>
        </tr>

    <?php endforeach;
endif; ?>

</tbody>

更新控制器中的代码

    if ($this->input->method()=="post") {
  if ($id) {
   $data1 = ['invoice_detail_subs'=> $this->input->post('invoice_detail_subs'),
   'invoice_detail_desc' => $this->input->post('invoice_detail_desc'),
'invoice_detail_interval' => $this->input->post('invoice_detail_interval'),
'invoice_detail_amount' => $this->input->post('invoice_detail_amount'),
 'invoice_detail_mddt' => date("Y-m-d H:i:s")];
$this->db->update($this->db->dbprefix."invoice_details", $data1,['invoice_detail_invoie_id'=>$id]);
 }
}
$query = $this->db->select('*')->where(["invoice_detail_invoie_id"=>$id])->get($this->db->dbprefix."invoice_details");
 $fetch = $query->row();
 $rows = $query->num_rows();

【讨论】:

  • 感谢您的回复。但我需要在该 foreach 循环中获取多个行值。这就是问题所在。
  • $fetch = $query->row();将其替换为 $fetch = $query->result();并在视图中通过 is_array 更改 is_object
猜你喜欢
  • 2021-08-03
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
相关资源
最近更新 更多