dongyuan7110 2016-09-15 18:29
浏览 48
已采纳

导出到CSV时操纵数据?

I'm using CodeIgniter v2.2.4

Consider the following code to export/download a CSV file representing results from a database query.

CONTROLLER:

public function export_csv($id = NULL)
{   
    $this->load->dbutil();
    $this->load->helper('download');

    $query = $this->my_model->create_csv($id);  // call the model

    $data = $this->dbutil->csv_from_result($query, ',');

    force_download( $id . '.csv', $data );
}

MODEL:

public function create_csv($id)
{
    $this->db->from('mytable');

    $this->db->where('id', $id);

    $this->db->select('
        id            AS `ID`,
        full_name     AS `Full Name`,
        company_name  AS `Company Name`,
        phone         AS `Phone Number`,
        select_list   AS `User Options`
    ', FALSE);

    return $this->db->get();
}

The code above is working, however the select_list value is 0, 1, or 2 and that's inserted into the CSV export. How can I manipulate these values into more meaningful text for my final CSV file?

Example:

'DB value' => 'insert into CSV'
         0 => 'N/A',
         1 => 'foo',
         2 => 'bar'
  • 写回答

2条回答 默认 最新

  • dongxing1965 2016-09-15 19:42
    关注

    Easiest way is to use a select statement along these lines

    $this->db->select(
       "id            AS `ID`,
        full_name     AS `Full Name`,
        company_name  AS `Company Name`,
        phone         AS `Phone Number`,
        CASE select_list WHEN = 1 THEN 'foo' WHEN = 2 THEN 'bar' ELSE 'N/A' END
        AS `User Options`", FALSE);
    

    CANCEL THAT! It is wrong.

    It should be as follows when using the optional expression after CASE, i.e. CASE select_list ...

    $this->db->select(
       "id            AS `ID`,
        full_name     AS `Full Name`,
        company_name  AS `Company Name`,
        phone         AS `Phone Number`,
        CASE select_list WHEN 1 THEN 'foo' WHEN 2 THEN 'bar' ELSE 'N/A' END
        AS `User Options`", FALSE);
    

    I was mixing the different forms the statement can use in the first (incorrect) example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题