dongruan6001 2014-01-09 18:56
浏览 29
已采纳

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

I use Ignited Table and I would like to combine 2 filed results in one column.

$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');

with this query I would have a table like

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

What I would like to achieve

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

is ignited table ablle to handle this?

  • 写回答

1条回答 默认 最新

  • drsxzut183207938 2014-01-09 20:27
    关注

    While viewing the code at Ignited-Datatables / application / libraries / Datatables.php i found the select function of ignited tables it actually used codeigniter's active library so you can use the mysql's CONCAT(str1,str2,...) function

    /* 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;
    }
    

    you can try this one by adding second parameter $backtick_protect as 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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?