在此代码中,我将从数据库获取状态值1或0。如果状态值为1,我想显示活动,否则不活动。有人知道吗?
控制器
public function datatable() {
$this->datatables
->select("prd_id,prd_name,status")
->from('jil_products')
->edit_column('status', '$1', $this->custom_status('status'));
echo $this->datatables->generate();
}
我为此回复了
function custom_status($val)
{
return ($val == 1) ? 'Active' : 'Inactive';
}
但如果值为1,则始终返回“非活动”。我不知道为什么
答案 0 :(得分:1)
试试这个:
public function datatable() {
/* Option 1 when you have 2 option */
$this->datatables
->select("prd_id,prd_name,IF(status = '1', 'Active', 'Inactive') as status")
->from('jil_products');
/* Option 2 when you have more then 2 option */
$this->datatables
->select("prd_id,prd_name,
case jil_products.status
when '1' then 'Active'
when '2' then 'Inactive'
when '3' then 'Suspended'
end as status
")
->from('jil_products');
echo $this->datatables->generate();
}