我有一个表分销商和用户。现在,每个用户都由他的经销商映射为一个名为KEY的字段,该字段唯一地定义了经销商!现在我有一个编辑用户的表单。在这里,如果经销商将用户的余额设置为yes,那么我想从经销商的余额字段中减去5欧元。下面是我的代码:
控制器检查经销商是否将余额设置为yes:
if($this->input->post('balance') === "Yes")
{
$this->reseller_m->deduct();
}
Mode函数,该函数应该从reseller balance中减去5:
public function deduct($balance)
{
$this->db->set('balance', 'balance-5');
$this->db->where('id' , $id);
$this->db->update('reseller',$data);
}
编辑代码:
public function edit ($id = NULL)
{
$usertype=$this->session->userdata('usertype');
if($usertype ==="reseller")
{
// Fetch a user or set a new one
if ($id) {
$this->data['user'] = $this->user_m->get($id);
count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
}
else {
$this->data['user'] = $this->user_m->get_new();
}
// Set up the form
$rules = $this->user_m->rules_admin;
$id || $rules['password']['rules'] .= '|required';
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->user_m->array_from_post(array('sip_id','sip_pass','name','email', 'password','phone','status','created','balance'));
$data['password'] = $this->user_m->hash($data['password']);
$this->user_m->save($data, $id);
if($this->input->post('balance') === "Yes")
{
$this->reseller_m->deduct();
//echo "goimng";
}
redirect('reseller/user');
}
// Load the view
$this->data['subview'] = 'reseller/user/edit';
$this->load->view('reseller/_layout_main', $this->data);
}
else{
$this->load->view('permission');
}
}
帮我从转售商减去5只有当他编辑自己的用户,不应该影响别人。
在您的模型中尝试此操作,其中$id
是经销商的id。
public function deduct($id)
{
$this->db->set('balance', 'balance-5', false);
$this->db->where('id' , $id);
$this->db->update('reseller');
}
同样,在你的控制器中,你必须知道经销商的id,并在调用模型函数时使用它:
if($this->input->post('balance') === "Yes")
{
$this->reseller_m->deduct($id); //you must have the $id (reseller's id) value set
}
set()还将接受可选的第三个参数($escape),如果设置为FALSE,将防止数据被转义。
使用FALSE
作为集合
$this->db->set('balance', 'balance-5',FALSE);
也不能在函数中定义$id and $data
并将更新更改为
$this->db->update('reseller');
最后的函数是
public function deduct($id)
{
$this->db->set('balance', 'balance-5',FALSE);
$this->db->where('id' , $id);
$this->db->update('reseller');
}
控制器if($this->input->post('balance') === "Yes")
{
$this->reseller_m->deduct($id);/// pass youe id here
//echo "goimng";
}
尝试一下$this->db->set('balance', 'balance-5', FALSE);
为了使值动态,我使用了类似于产品库存更新的方法
这里,$purchase_qty
是订购的项目。$product_id
为订货产品。
$this->db->set("stock", "stock - $purchase_qty", FALSE)
->where('price_id', $price_id)
->where('product_id', $product_id)
->update('price_table');
希望这对你有帮助。