我想在codeigniter中使用点亮的数据表显示基于数据库值的一些值

时间:2016-02-26 06:23:44

标签: codeigniter datatables codeigniter-2 codeigniter-3

在此代码中,我将从数据库获取状态值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,则始终返回“非活动”。我不知道为什么

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();
 }
相关问题