对于leave_type = 11
效果很好,但是对于leave_type = 3
它仍然插入0不知道怎么解决
leave_type = 3
为半天leave_type = 11
为短假日
'days' => ($this->input->post('leave_type') == 3) ? 0.5 : ($this->input->post('leave_type') == 11) ? 0 :$days_between ,
请提供解决方案
这是因为您没有对三元运算符进行分组。
($this->input->post('leave_type') == 3) ? 0.5 : (($this->input->post('leave_type') == 11) ? 0 :$days_between)
^ ^
这里的解释是因为在原始代码中,它首先计算左边的条件,即($this->input->post('leave_type') == 3) ? 0.5 : (($this->input->post('leave_type') == 11)
在您的情况下,应该是0.5
。但是,由于右边还有另一个三元操作符,因此0.5
将被转换为布尔类型,即true
。因此,由于传递给下一个三元运算符的求值是true,因此返回值将是0
。
true ? 0 : $days_between
请尝试如下:
'days' => ($this->input->post('leave_type') == 3) ? '0.5' : (($this->input->post('leave_type') == 11) ? 0 :$days_between) ,
这与您的数据库字段数据类型有关。您必须将该字段数据类型设置为整数。你需要将它设置为浮动