codeigniter:如果'null'值设置为0

时间:2017-10-03 14:34:05

标签: php codeigniter null

代码中的

是我想要的,文件是控制器,还是应该在模型中进行操作?

public function newUser(){
    $param['name'] = $this->input->post("name");
    $param['email'] = $this->input->post("email");
    $param['user'] = $this->input->post("user");
    $param['password'] = sha1($this->input->post("password"));
    $param['state'] = $this->input->post("state"); //<-- this value is a checkbox, if is unckecked the value became 'null'. What I want is to change that 'null' value to 0 before to send to the database.

    $this->mdatabase->newUser($param);
}

谢谢:D。

1 个答案:

答案 0 :(得分:0)

通过使用isset,我们可以检查变量是否设置为任何值,或者为null。然后,从那里,我们可以应用逻辑来相应地分配您的值。

 public function newUser(){
        $param['name'] = $this->input->post("name");
        $param['email'] = $this->input->post("email");
        $param['user'] = $this->input->post("user");
        $param['password'] = sha1($this->input->post("password"));
        if(!isset($param['state'])
           $param['state'] = 0;
        else if(isset($param['state'])
           $param['state'] = $this->input->post("state");

        $this->mdatabase->newUser($param);
    }

应该为你做的伎俩。让我知道它是如何工作的。

修改:出现语法错误。现在应该工作。