This my table that is has a foreign key in my tbl_worklog
Hello good day every one i want to ask who had an idea on how to minus the my budget amount in my tbl_budget so the process here is this when the user select in the dropdown each dropdown value has a budget amount corresponds when the user will pick a value it displays a amount the the approved allocation field then when the user will put a PO amount when the user click the add record it will automatically minus the amount according to the po amount that is the user inputted.
This is my Controller that will get the data in budget id,name,amount and other table that i joined public function index() { $sessionid = $this->session->userdata('sessionid'); if (empty($sessionid)) { redirect('login'); } else { $data['logs'] = $this->m->getlog(); $this->load->view('layout/header'); $this->load->view('addrecord'); } } This is my submit function that will show a flashdata public function submit() { $result = $this->m->submit(); // return $result; if ($result) { $this->session->set_flashdata('success_msg', 'Record Added Successfully!'); } else { $this->session->set_flashdata('error_msg', 'Fail to add record!'); } redirect(base_url('worklog')); } This function is used to fetch the bugdet id and it can fetch the amount in the tbl_budget table to dispplay in the budget allocation field. public function check_budget() { $sessionid = $this->session->userdata('sessionid'); if (empty($sessionid)) { redirect('login'); } if (!empty($this->input->post('budget_id'))) { $budget_id = $this->input->post('budget_id'); $data = $this->m->getbudget($budget_id); if (!empty($data)) { echo json_encode(array("data" => $data)); } else { echo json_encode(array("data" => "")); } } } Model This is my model that has a function to insert the record into the database public function getlog() { $query = $this->db->query('select * from tbl_worklog left join tbl_file on tbl_worklog.id=tbl_file.worklog_id left join tbl_budget ON tbl_worklog.`budget_id` = tbl_budget.`budget_id`'); return $query->result(); } // this function is use to fetch the budget id public function getbudget($budget_id) { $this->db->select('*'); $this->db->from('tbl_budget'); $this->db->where('budget_id', $budget_id); $query = $this->db->get(); if ($query->num_rows() > 0) { return $query->result(); } else { return false; } } public function submit() { // return var_dump($this->session->userdata("sessionid")); $field = array( 'budget_id' => $this->input->post('budget_id'), 'po_amount' => $this->input->post('po_amount'), ); $this->db->insert('tbl_worklog', $field); } This is my view where the user can see a dropdown and select a budget account and each budget account has it owns amount so when the user select one of a value in the dropdown after selecting it will display a mount in the approved budget allocation <div class = "form-group"> <label >Account title/Budget Account</label> <select name='budget_id' id="budget_id" class='form-control'> <option value="">Select Budget Account</option> <option value="1">50203010A - Office Supplies Expenses - Regular</option> <option value="2">50203080 - Medical,Dental and Laboratory Supplies Expenses</option> </select> </div> <div class="form-group"> <label for="Approved Budget Allocation">Approved Budget Allocation</label> <input type="text" name="aba" class="form-control" id="aba" disabled=""> </div> <div class="form-group"> <label for="Procurement Amount">PO Amount</label> <input type="text" name="po_amount" class="form-control" id="proc_amount" placeholder=""> </div> JavaScript this is the fetching of the budget so if the user will pick a budget account this script will display the amount in the budget allocation field function check_budget() { $('#budget_id').on('change', function (e) { var log_id = $('#id').val(); var budget_id = $('#budget_id').val(); var base_url = $('#base_url').val(); var pathname = window.location.pathname; if (pathname === "/prpotracker/update/edit/" + log_id) { var url = "update/check_budget"; } else { var url = "addrecord/check_budget"; } $.ajax({ type: "POST", url: base_url + url, async: true, dataType: 'JSON', data: {budget_id: budget_id}, success: function (data) { for (var i = 0; i < data.data.length; i++) { $('#aba').val(format_commas(data.data[i].amount)); } }, }); }); } https://stackoverflow.com/questions/67051826/how-to-minus-amount-in-codeigniter April 12, 2021 at 10:04AM
没有评论:
发表评论