douliu3831 2015-09-02 11:46
浏览 69
已采纳

如何从PHP codeigniter中的字段中减去一个值?

i am have a table Reseller and Users. Now Each user is mapped by his Reseller with a field called KEY which uniquely defines the reseller! now I have a form where i edit a user. in here if reseller sets the balance of user to yes then i want to subtract 5 euros from the balance field of Reseller. Below is my code:

the controller to check if reseller has set balance to yes:

 if($this->input->post('balance') === "Yes")
         {
            $this->reseller_m->deduct();
         }

the Mode function which should subtract 5 from reseller balance:

public function deduct($balance)
{
    $this->db->set('balance', 'balance-5');
    $this->db->where('id' , $id);
    $this->db->update('reseller',$data);
}

the edit code :

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

}

help me to subtract 5 from reseller only when he edits his own user and should not affect others.

  • 写回答

3条回答 默认 最新

  • dongluolie3487 2015-09-02 12:01
    关注

    Try this in your model, where $id is the id of the reseller.

    public function deduct($id)
    {
        $this->db->set('balance', 'balance-5', false);
        $this->db->where('id' , $id);
        $this->db->update('reseller');
    }
    

    Alse, in your controller you must know the reseller's id, and use it when you call the model function:

     if($this->input->post('balance') === "Yes")
             {
                $this->reseller_m->deduct($id); //you must have the $id (reseller's id) value set
             }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#linux#的问题(输入输出错误):出现这个界面接着我重新装系统,又让修电脑的师傅帮我扫描硬盘(没有问题)用着用着又卡死(相关搜索:固态硬盘)
  • ¥15 cv::resize不同线程时间不同
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题