1

当我选择其中一个复选框时,我遇到了问题。如果未选中复选框,如何停止 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(); ?>
4

2 回答 2

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

        }
    }
}
于 2013-08-23T08:28:25.683 回答
0

如果我正确理解您的问题,请将您的 foreach 放入以下代码中

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

}
于 2013-08-23T06:23:32.963 回答