复选框更新减去数量codeigniter

时间:2013-08-22 08:37:25

标签: codeigniter

当我选择其中一个时,我的复选框有问题。如果未选中复选框,如何阻止foreach运行?请检查我的代码以解决问题。

我想要一个未经检查的数据条件。

这是我的控制器:

public function create_cargo_manifest(){
   $core_model = new Core_m;
   $core_model->save_cargo_details();

   redirect('core/cargo_lookup/','refresh');
}

这是我的模特:

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 = $qty - $data['quantity'];


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

        }
    }
}

这是我的观点:

<?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; ?><input type="hidden" ></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 } ?>
</table>
     <input type="submit" value="Save"><button id="button_cancel" type="button">Close</button> 
<?php form_close(); ?>

2 个答案:

答案 0 :(得分:1)

问题在于您的$waybillno[]$quantity[]等数组。是不相关的,所以你不能假设具有相同索引的值对应于它们在表单中的提交方式,我会设置输入的name属性,使它们形成一个单一的多维像这样的数组:

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

然后你需要像这样更新模型函数:

function save_cargo_details() {
    $headers = $this->input->post('headers');

    foreach($headers as $header){
        if(isset($header['waybillno'])){//check for selected checkbox
            $data = array(
                'waybillno' => $header['waybillno'],
                'quantity' => $header['quantity'],
                'waybilldate' => $header['waybilldate'],
                'declared_value' => $header['declared_value'],
                'consignee' => $header['consignee'],
            );

            //UPDATE QUERY can be shortened to
            $this->db->where('waybillno',$header['waybillno']);
            $this->db->set('remaining_qty','remaining_qty-'.$header['quantity'],FALSE);
            $this->db->update(self::WAYBILL_DETAILS_TABLE);
            // INSERT DATA //
            $this->db->insert('sys_cargodetails', $data);

        }
    }
}

答案 1 :(得分:0)

如果我理解你的问题 把你的foreach放在下面的代码中

if(count($waybill_header)>0){
   //your foreach loop

}