douhu4692 2013-09-09 20:46 采纳率: 100%
浏览 52
已采纳

CodeIgniter向CSV添加字段

I have this code below and what it does is it generates a CSV from data from the database and it works fine. My only issue is that I want to add another field to the CSV (in the middle and not in the end if possible) is this possible? the field would take the DOB from the data and give that person a title based on age (born after 1994 they are 'Junior' born after they are 'Senior') and so on, there are like 4 or 5 different titles an age can be. I hope this makes sense.

function csv_all_members(){

                $this->load->dbutil();
                $this->db->select("first_name as 'First Name', last_name as 'Last Name', phone as 'Phone', os.group as 'Group', gender as 'Gender', birth_date as 'DOB', email as 'Email', street_address as 'Address', city as 'City', province as 'Province', postal_code as 'Postal Code', country 'Country', payment_amount as 'Payment Amount', DATE_FORMAT(payment_date, '%d/%m/%Y') as 'Payment Date', an.notes as 'Notes'", false);
                $this->db->from('offline_shoppers os');
                $this->db->join('athlete_notes an', 'os.id = an.id', 'inner');
                $this->db->group_by(array("first_name", "last_name"));
                $this->db->order_by("last_name asc, first_name asc");
                $query = $this->db->get();
                header("Content-type: text/csv");
                header("Content-Disposition: attachment; filename=members_list.csv");
                header("Pragma: no-cache");
                header("Expires: 0");
                echo $this->dbutil->csv_from_result($query);
        }
  • 写回答

1条回答 默认 最新

  • dousheyan0375 2013-09-09 20:55
    关注

    Just add it to the select statement:

    $this->db->select("IF(year(birth_date) > 1994, 'Junior, 'Senior') AS Title "
    

    All together it looks like this:

    $this->db->select("
        first_name as 'First Name',
        last_name as 'Last Name',
        phone as 'Phone',
        IF(year(birth_date) > 1994, 'Junior, 'Senior') AS Title,
        os.group as 'Group',
        gender as 'Gender',
        birth_date as 'DOB',
        email as 'Email',
        street_address as 'Address',
        city as 'City',
        province as 'Province',
        postal_code as 'Postal Code',
        country 'Country',
        payment_amount as 'Payment Amount',
        DATE_FORMAT(payment_date, '%d/%m/%Y') as 'Payment Date',
        an.notes as 'Notes'");
    

    Edit: For 'else if' situations use:

        (
        CASE 
            WHEN year(birth_date) > 1994 THEN 'Junior'
            WHEN year(birth_date) > 2000 THEN 'Jr. Junior'
            WHEN year(birth_date) > 2012 THEN 'Jr. Jr. Junior'
            ELSE 'Senior'
        END) AS Title
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 找人不需要人工智能回答的gamit解算后做形变分析
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错