如何使用Codeigniter更新表中的确切行

时间:2019-07-13 09:14:42

标签: php codeigniter row

我的查看页面: enter image description here

在这张照片中,我正在使用loan no获取partynamecoll.amtdate之类的数据(即,它显示了我给定的那个特定日期的数据,来自“集合”表)

如果我输入了loan no 2的代表,但我没有给loan no 1、3。当我按下ok button时,代表的代表应更新为“ collecion”表以获取确切的贷款编号和日期。

我的模型代码:

public function batchinsert($data){
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['repname'];
    $LDate = $this->input->post('CDate');
    $date = str_replace('/', '-', $LDate);
    $newDate = date("Y-m-d", strtotime($date));
    $lno = $this->input->post("Sno");
    $count = count($data['Sno']);
    for($i = 0; $i<$count; $i++){ 
        $entries2[] = array(               
            'receive_amt'=>$data['ramt'][$i],

            ); 
    }
    $this->db->where('loanno',$lno);
    $this->db->where('collection_date',$newDate);
    $this->db->update_batch('collection',$entries2);
    //        $this->db->insert_batch('test', $entries2);
    redirect('Collection_Entry','refresh');
}

我的控制器代码:

public function Collection_Insert(){
    $this->load->model('User_model');
    $result = $this->User_model->batchinsert($_POST);
}

“我的查看”页面代码:

<table class="table table-bordered table-striped table-xxs" id="tb3">
    <thead>
        <tr>
            <th>Loan No</th>
            <th>Party Name</th>
            <th>Coll.Amt</th>
            <th>Rep Amt</th>
        </tr>
    </thead>
    <tbody>
    <?php
    //echo '<pre>';print_r($result2);exit();
    if(!empty($query)){
        foreach($query as $row){
    ?>
        <tr >
        <td ><input style="width:50px" type="text" class="form-control input-xs" name="Sno[]" id="Sno" value="<?=$row['loanno'];?>"></td>

        <td> <input style="width:180px" type="text" class="form-control input-xs" name="name[]" id="Amount" value="<?=$row['pname'];?>"></td>

        <td ><input style="width:80px" type="text" class="form-control input-xs amt" name="Amount[]" id="Bankname" value="<?=$row['collection_amt'];?>"></td>

        <td ><input style="width:80px" type="text" class="form-control input-xs ramt" name="ramt[]" id="Chqamt" value="<?=$row['receive_amt'];?>" autofocus></td>
        </tr>

    <?php
        }
    }?> 
    </tbody>
</table>

请解决这个问题。

1 个答案:

答案 0 :(得分:1)

如果您只想对ramt[]输入中具有非空值的内容进行更新,则可以为每个输入的文本名称分配一个键,以便以后进行识别:

<table class="table table-bordered table-striped table-xxs" id="tb3">
    <thead>
            <tr>
    <th>Loan No</th>
    <th>Party Name</th>
    <th>Coll.Amt</th>
    <th>Rep Amt</th>
    </tr>
    </thead>
    <tbody>
        <?php
        //echo '<pre>';print_r($result2);exit();
        if(!empty($query)){
            foreach($query as $key => $row){ // added $key as index
            ?>
                <tr >
                    <td ><input style="width:50px" type="text" class="form-control input-xs" name="Sno[<?php echo $key ?>]" id="Sno" value="<?=$row['loanno'];?>"></td>

                    <td> <input style="width:180px" type="text" class="form-control input-xs" name="name[<?php echo $key ?>]" id="Amount" value="<?=$row['pname'];?>"></td>

                    <td ><input style="width:80px" type="text" class="form-control input-xs amt" name="Amount[<?php echo $key ?>]" id="Bankname" value="<?=$row['collection_amt'];?>"></td>

                    <td ><input style="width:80px" type="text" class="form-control input-xs ramt" name="ramt[<?php echo $key ?>]" id="Chqamt" value="<?=$row['receive_amt'];?>" autofocus></td>
                </tr>

            <?php
            }
        }?> 
    </tbody>
</table>

并过滤batchinsert()函数上的输入数据:

public function batchinsert($data){
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['repname'];
    $LDate = $this->input->post('CDate');
    $date = str_replace('/', '-', $LDate);
    $newDate = date("Y-m-d", strtotime($date));
    $lno = $this->input->post("Sno");
    $ramt = $this->input->post("ramt"); // added ramt input variable
    $count = count($data['Sno']);
    $updateArray = array();
    for($x = 0; $x < sizeof($lno); $x++){

        if (!empty($ramt[$x])) { // this will only insert data on loanno which have non-empty ramt values
            $updateArray[] = array(
                'loanno' => $lno[$x],
                'receive_amt'=> $ramt[$x]
            );
        }
    }
    $this->db->where('collection_date',$newDate);
    $this->db->update_batch('collection',$updateArray,'loanno');
    //        $this->db->insert_batch('test', $entries2);
    redirect('Collection_Entry','refresh');
}

这只会循环通过非空的ramt[]输入,然后仅更新动态loanno值和静态collection_date值。

// Example query output : 
// UPDATE `collection`
// SET
// `receive_amt` = 
// CASE 
//     WHEN `loanno` = '1' THEN 1 
//     WHEN `loanno` = '3' THEN 2 
//     WHEN `loanno` = '6' THEN 3 
//     WHEN `loanno` = '17' THEN 4 
//     ELSE `receive_amt`
// END 
// WHERE `collection_date` = '2019/01/09' AND `loanno` IN ('1','3','6','17')