Codeigniter Ignited Table将2个查询字段结果组合在一列中

时间:2014-01-09 18:56:47

标签: php codeigniter datatable

我使用Ignited Table,我希望将两个归档结果合并到一列中。

$this->datatables
            ->select('ci_orders.id,'
                    .'ci_orders.transaction_id,'
                    .'ci_orders.field1,'
                    .'ci_orders.field2,'
                    .'ci_orders.status')
            ->from('ci_orders')
            ->join('ci_users', 'ci_orders.user = ci_users.id')
            ->where('ci_users.id',$userId)
            ->unset_column('ci_orders.id')
            ->add_column('edit', '<a href="' . base_url() . 'cancel_order/$1"><button class="btn btn-danger" type="button"><i class="fa fa-times-circle"></i> Cancel</button>', 'id');

使用此查询我会有一个类似

的表
---transaction_id---|---field_header1---|---field_header2---|---status---
   some id          |   value1          |   value2          | pending   

我想要实现的目标

---transaction_id---|---field_header---|---status---
   some id          |   value1/value2  | pending   

被点燃表ablle来处理这个?

1 个答案:

答案 0 :(得分:3)

Ignited-Datatables / application / libraries / Datatables.php查看代码时,我找到了被点击表的select函数,它实际上使用了codeigniter的活动库,因此你可以使用mysql的CONCAT(str1,str2,...)函数

/* demo code just to show ,taken from ignited tables*/
public function select($columns, $backtick_protect = TRUE)
{
  foreach($this->explode(',', $columns) as $val)
  {
    $column = trim(preg_replace('/(.*)\s+as\s+(\w*)/i', '$2', $val));
    $this->columns[] =  $column;
    $this->select[$column] =  trim(preg_replace('/(.*)\s+as\s+(\w*)/i', '$1', $val));
  }

  $this->ci->db->select($columns, $backtick_protect);
  return $this;
}

你可以通过添加第二个参数$backtick_protect作为FALSE

来尝试这个
$this->datatables
     ->select("ci_orders.id,ci_orders.transaction_id,
     CONCAT(ci_orders.field1, '  ', ci_orders.field2) AS ci_orders.field1,
     ci_orders.status",FALSE)
     ->from('ci_orders')
     ->join('ci_users', 'ci_orders.user = ci_users.id')
     ->where('ci_users.id',$userId)
     ->unset_column('ci_orders.id')
     ->add_column('edit', '<a href="' . base_url() . 'cancel_order/$1"><button class="btn btn-danger" type="button"><i class="fa fa-times-circle"></i> Cancel</button>', 'id');