猿问

插入具有两个值代码点火器的复选框

在我看来:


<form  method="post" action="<?php echo base_url(); ?>Data/Penilaian/Selected">

  <table class="table datatable-responsive" id="example">

    <thead>

      <tr>

      <th><button class="btn btn-info">Submit</button></th>

      <th>No</th>

       <th>Kendaraan</th>

       <th>Plat Nomor</th>

      <th>Total</th>  

      </tr>

    </thead>

    <tbody>

    <?php 

      $no=1; 

      if(!empty($getsifat)) {

        foreach ($getsifat as $kr_key => $kendaraan) { ?>

        <tr>

          <td>

          <?php foreach ($kendaraan['sub']['count'] as $data)     { ?>

            <input type="checkbox" name="id_kendaraan[]" value="<?php echo $kendaraan['id_kendaraan'] ?>">

            <input type="hidden" name="total[]" value="<?php echo $data['total'] ?>" > //this my problem

          <?php } ?>

          </td>

          <td><?php echo $no ?> </td>

          <td><?php echo $kendaraan['nama'] ?> </td>

          <td><?php echo $kendaraan['platno'] ?></td>

          <?php

          foreach ($kendaraan['sub']['count'] as $data)     { ?>

          <!-- <input type="hidden" name="total[]" value="<?php echo $data['total'] ?>"> -->

          <td><?php echo $data['total'] ?></td>

          <?php } ?>

        </tr> 

    <?php 

      $no++; 

        }

      } 

    ?>

    </tbody>

  </table>

  </form>

在视图中我想插入$id_kendaraan和$total值


如果我检查了 $id_kendaraan 2 并按提交,在我的数据库中数据 $id_kendaraan value = 2, $total value = 20, 是不正确的......在我的数据库中这个数据应该是 $id_kendaraan value = 2, $total value = 35


如果我尝试检查 $id_kendaraan 3 并按提交,在我的数据库中数据 $id_kendaraan value = 3, $total value = 20, 不正确...在我的数据库中这个数据应该是 $id_kendaraan value = 3, $total value = 44


如果我尝试检查 $id_kendaraan 2,3 并按提交,在我的数据库中数据 $id_kendaraan value = 2, $total value = 20 and $id_kendaraan value = 3, total value = 35, is not correct... in my db此数据应为 $id_kendaraan 值 = 2,$total 值 = 35 和 $id_kendaraan 值 = 3,$total 值 = 44


谢谢你的帮助


MYYA
浏览 85回答 1
1回答

慕田峪4524236

它正在发生,因为隐藏数组将发送所有数据,但您的复选框将仅发布选定的数组值。因此,您的服务器端代码中会发生密钥不匹配。要解决这个问题,您需要唯一地提供数组键。重新排列您的前端代码,如下所示。<?php&nbsp;&nbsp; $no=1;&nbsp;&nbsp; if(!empty($getsifat)) {&nbsp; &nbsp; $inputid = 0;&nbsp; &nbsp; foreach ($getsifat as $kr_key => $kendaraan) {&nbsp;&nbsp; &nbsp; &nbsp; $attch1 = $attch2 = "";&nbsp; &nbsp; &nbsp; foreach ($kendaraan['sub']['count'] as $data){&nbsp; &nbsp; &nbsp; &nbsp; $attch1 .= '<input type="checkbox" name="id_kendaraan['.$inputid.']" value="'.$kendaraan['id_kendaraan'].'">';&nbsp; &nbsp; &nbsp; &nbsp; $attch1 .= '<input type="hidden" name="total['.$inputid.']" value="'.$data['total'].'" >';&nbsp; &nbsp; &nbsp; &nbsp; $attch2 .= '<td>'.$data['total'].'</td>';&nbsp; &nbsp; &nbsp; &nbsp; $inputid++;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ?>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td><?php echo $attch1 ?> </td>&nbsp; &nbsp; &nbsp; <td><?php echo $no ?> </td>&nbsp; &nbsp; &nbsp; <td><?php echo $kendaraan['nama'] ?> </td>&nbsp; &nbsp; &nbsp; <td><?php echo $kendaraan['platno'] ?></td>&nbsp; &nbsp; &nbsp; <?php echo $attch2 ?>&nbsp; &nbsp; </tr>&nbsp;<?php&nbsp;&nbsp; $no++;&nbsp;&nbsp; &nbsp; }&nbsp; }&nbsp;?>像这样重写控制器&nbsp;public function Selected(){&nbsp; &nbsp; $id_kendaraan = $this->input->post('id_kendaraan');&nbsp; &nbsp; $total = $this->input->post('total');&nbsp; &nbsp; $data = array();&nbsp; &nbsp; foreach ($id_kendaraan as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; $data = array(&nbsp; &nbsp; &nbsp; &nbsp; 'id_kendaraan' =>&nbsp; $value,&nbsp; &nbsp; &nbsp; &nbsp; 'total' =>&nbsp; $total[$key]&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $this->PenilaianModel->TambahSelected($id_kendaraan, $data);&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; redirect('Data/Penilaian', 'refresh');&nbsp; }笔记 :您正在循环两次相同的循环。我把它作为一个。in line 您正在循环打印。我猜它会出现在循环之外。插入上下文中不需要行$this->db->where('id_kendaraan', $id_kendaraan)。
随时随地看视频慕课网APP
我要回答