0

当我使用复选框更新每一个或一个数据时,我遇到了一些问题。我想做的是只更新选中的复选框。如果未选中复选框,如何停止 foreach 运行。

这是我的模型

function save_cargo_details() {
    $data = array();

    $waybillno = $this->input->post('waybillno');
    $quantity = $this->input->post('quantity');
    $waybilldate = $this->input->post('waybilldate');
    $declared_value = $this->input->post('declared_value');
    $consignee = $this->input->post('consignee');


    $count = count($waybillno);



    if(empty($waybillno)){

    }else{



        for ($i = 0; $i < $count; $i++) {
            $data = array(
                'waybillno' => $waybillno[$i],
                'quantity' => $quantity[$i],
                'waybilldate' => $waybilldate[$i],
                'declared_value' => $declared_value[$i],
                'consignee' => $consignee[$i],

            );

            // SUBRACT REMAINING_QUANTITY //


            $this->db->select('wd.remaining_qty');
            $this->db->where('wd.waybillno',$waybillno[$i]);
            $this->db->from(self::WAYBILL_DETAILS_TABLE. " as wd");
            $query = $this->db->get()->row();
            $qty = $query->remaining_qty; 

            $remaining = abs($data['quantity'] - $qty);

           $this->db->where('waybillno',$waybillno[$i]);
           $this->db->set('remaining_qty',$remaining);
           $this->db->update(self::WAYBILL_DETAILS_TABLE);
           // INSERT DATA //
           $this->db->insert('sys_cargodetails', $data);
           $this->session->set_flashdata('success', '<p id="success_message">Record has been successfully saved.</p>');

        }
    }
 }

这是我的控制器

public function create_cargo_manifest(){
   $core_model = new Core_m;
   $core_model->save_cargo_details();
   redirect('core/cargo_lookup/');
}

这是我的看法

<?php foreach($waybill_header as $waybill_header) { ?>
   <?php echo form_open('core/create_cargo_manifest'); ?>
<tr style="text-align: center;">
    <td><input type="checkbox" name="waybillno[]" value="<?php echo $waybill_header->waybillno; ?>"></td>
    <td><?php echo $waybill_header->waybillno; ?></td>
    <td><?php echo $waybill_header->waybilldate; ?><input type="hidden" value="<?php echo $waybill_header->waybilldate; ?>" name="waybilldate[]"></td>
    <td><input type="text" size="5" value="<?php echo $waybill_header->remaining_qty; ?>" name="quantity[]">
        <input type="hidden" value="<?php echo $waybill_header->declared_value; ?>" name="declared_value[]">
        <input type="hidden" value="<?php echo $waybill_header->consignee; ?>" name="consignee[]">
    </td>
</tr>



 <?php } ?>
4

1 回答 1

0

您可以在数据库中为复选框提供一个额外的字段。如果选中复选框,则将 1 作为值,如果未选中,则将 0 作为值。根据您的要求提供默认值。如果您希望检查每个复选框否则将 1 作为默认值。然后您只需在 foreach 循环之前放置一个条件,即如果复选框的值为 1,则运行 foreach 循环,否则不运行。

于 2013-08-27T19:47:19.953 回答