首页 > 解决方案 > How to deduct value from database codeigniter

问题描述

I'm currently having a probleming deducting the value from database to my field

Here's my code

In my model I have a function which is called deduct

public function deduct($id){

        $data = array(
            'loan' => $this->input->post('loan'),
            'id' => $id,
            'loan_type' => $this->input->post('loan_type'),
            'loan_amount' => $this->input->post('loan_amount'),
            'firstname' => $this->input->post('firstname'),
            'lastname' => $this->input->post('lastname'),
            'middlename' => $this->input->post('middlename'),
            'gender' => $this->input->post('gender'),
            'civilstatus' => $this->input->post('civilstatus'),
            'birthday' => $this->input->post('birthday'),
            'age' => $this->input->post('age'),
            'dependents' => $this->input->post('dependents'),
            'spousefirstname' => $this->input->post('spousefirstname'),
            'spouselastname' => $this->input->post('spouselastname'),
            'spousemiddlename' => $this->input->post('spousemiddlename'),
            'spouseage' => $this->input->post('spouseage'),
            'spousebirthday' => $this->input->post('spousebirthday'),
            'homeaddress' => $this->input->post('homeaddress'),
            'zipcode' => $this->input->post('zipcode'),
            'lengthofstay' => $this->input->post('lengthofstay'),
            'hometype' => $this->input->post('hometype'),
            'emailaddress' => $this->input->post('emailaddress'),
            'homephonenumber' => $this->input->post('homephonenumber'),
            'businessphonenumber' => $this->input->post('businessphonenumber'),
            'mobilenumber' => $this->input->post('mobilenumber'),
            'nameofbusiness' => $this->input->post('nameofbusiness'),
            'natureofbusiness' => $this->input->post('natureofbusiness'),
            'addressofbusiness' => $this->input->post('addressofbusiness'),
            'yearsofbusiness' => $this->input->post('yearsofbusiness'),
            'nameofcomaker' => $this->input->post('nameofcomaker'),
            'addressofcomaker' => $this->input->post('addressofcomaker'),
            'numberofcomaker' => $this->input->post('numberofcomaker'),
            'balance' => $this->input->post('cash_amount')
        );


        $this->db->set('loan_amount','loan_amount-balance', false);
        $this->db->where('id', $id);
        $this->db->update('employee',$data);
    }

and on my controller I have a pay function

public function pay($id)
    {
        $data = array();
        $this->template->set('title', 'Payment Section');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('cash_amount', 'cash_amount', 'required|callback_check_default');
        $this->form_validation->set_message('check_default','There are some fields that are required to fill up!');
        $this->form_validation->set_error_delimiters('<div class="alert alert-danger" id="hideMe">', '</div>');
        if ($this->form_validation->run() === FALSE)
        {
            $data["data"] = $this->employee->get_employees('', '', $id);
            $this->template->load('cashier_dashboard', 'contents' , 'payment', $data);
        }   
        else
        {
            $this->employee->deduct($id);
            redirect(base_url(). "home/cashier_dashboard");
        }
    }

and actually I have 2 views for my default layout and the forms for adding the cash

here's my default layout

<table class="table table-bordered table-hover" cellspacing="0" width="100%" id="employee">
        <thead>
            <tr>
                <th>Loan</th>
                <th>Loan Type</th>
                <th>Loan Amount</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Balance</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        <?php $i=1;foreach($data as $rec): ?>
        <tr>
            <td><?php echo $rec->loan; ?></td>
            <td><?php echo $rec->loan_type; ?></td>
            <td><?php echo $rec->loan_amount; ?></td>
            <td><?php echo $rec->firstname; ?></td>
            <td><?php echo $rec->lastname; ?></td>
            <td><?php echo $rec->balance?></td>
            <td>
                <div class="hidden-sm hidden-xs btn-group">
                    <a class="btn btn-xs btn-success" href="<?php echo site_url('home/pay/'.$rec->id); ?>">
                        <i class="ace-icon fa fa-rub bigger-120"></i>
                    </a>
                </div>
            </td>
        </tr>
        <?php endforeach; ?>
        </tbody>
    </table>

and on my form view

<?php echo form_open('home/pay/'.$data['id'], "class='form-horizontal'"); ?>

    <div class=form-group>
        <div class="row">
            <div class="col-sm-3">
                <input type="text" id="form-field-2" placeholder="Loan" name="loan" value="<?php echo $data['loan']; ?>" readonly>
            </div>
            <div class="col-sm-3">
                <input type="text" id="form-field-2" placeholder="Loan Type" name="loan_type" value="<?php echo $data['loan_type']; ?>" readonly>
            </div>
            <div class="col-sm-3">
                <input type="text" id="form-field-2" placeholder="Loan Amount" name="loan_amount" value="<?php echo $data['loan_amount']; ?>" readonly>
            </div>
            <div class="col-sm-3">
                <input type="text" id="form-field-2" placeholder="Cash Amount" name="cash_amount" value="<?php echo set_value('cash_amount'); ?>">
            </div>
        </div>
    </div>

    <div class="space-4"></div>
    <div class="clearfix form-actions">
        <div class="col-md-offset-3 col-md-9">
            <button class="btn btn-info center-block" type="submit">
                <i class="ace-icon fa fa-check bigger-110"></i>
                Submit
            </button>

            &nbsp; &nbsp; &nbsp;
            <button class="btn" type="reset">
                <i class="ace-icon fa fa-undo bigger-110"></i>
                Reset
            </button>
        </div>
    </div>
</form>


And on my database I've got a balance field and loan_amount field where in I need to deduct the loan_amount base on what I input on the cash_amount input on my view. but its currently not deducting

标签: phpmysqlcodeigniter

解决方案


$this->db->set('loan_amount','loan_amount-balance', false); it will not take effect because you set $data in $this->db->update('employee',$data);.

this will resolve your problem :)

public function deduct($id){

        $data = array(
            'loan' => $this->input->post('loan'),
            'id' => $id,
            'loan_type' => $this->input->post('loan_type'),
            'loan_amount' => $this->input->post('loan_amount') - $this->input->post('cash_amount'),
            'firstname' => $this->input->post('firstname'),
            'lastname' => $this->input->post('lastname'),
            'middlename' => $this->input->post('middlename'),
            'gender' => $this->input->post('gender'),
            'civilstatus' => $this->input->post('civilstatus'),
            'birthday' => $this->input->post('birthday'),
            'age' => $this->input->post('age'),
            'dependents' => $this->input->post('dependents'),
            'spousefirstname' => $this->input->post('spousefirstname'),
            'spouselastname' => $this->input->post('spouselastname'),
            'spousemiddlename' => $this->input->post('spousemiddlename'),
            'spouseage' => $this->input->post('spouseage'),
            'spousebirthday' => $this->input->post('spousebirthday'),
            'homeaddress' => $this->input->post('homeaddress'),
            'zipcode' => $this->input->post('zipcode'),
            'lengthofstay' => $this->input->post('lengthofstay'),
            'hometype' => $this->input->post('hometype'),
            'emailaddress' => $this->input->post('emailaddress'),
            'homephonenumber' => $this->input->post('homephonenumber'),
            'businessphonenumber' => $this->input->post('businessphonenumber'),
            'mobilenumber' => $this->input->post('mobilenumber'),
            'nameofbusiness' => $this->input->post('nameofbusiness'),
            'natureofbusiness' => $this->input->post('natureofbusiness'),
            'addressofbusiness' => $this->input->post('addressofbusiness'),
            'yearsofbusiness' => $this->input->post('yearsofbusiness'),
            'nameofcomaker' => $this->input->post('nameofcomaker'),
            'addressofcomaker' => $this->input->post('addressofcomaker'),
            'numberofcomaker' => $this->input->post('numberofcomaker'),
            'balance' => $this->input->post('cash_amount')
        );

        $this->db->where('id', $id);
        $this->db->update('employee',$data);
    }

推荐阅读