在foreach循环中以表格形式修改数组数据

时间:2018-06-27 12:07:08

标签: php arrays codeigniter foreach

视图页面中有一个表单,该表单将数据作为多维数组。在编辑模式下,表单值是通过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);


    }
}

1 个答案:

答案 0 :(得分:0)

查看代码

<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();