Codeigniter:クエリからCSVダウンロードした時のCSVデータを変更

CodeigniterでクエリからCSVダウンロードを行った際に、データベースユーティリティクラスの「csv_from_result」を使用してCSVデータを取得しました。
「csv_from_result」で取得したデータを参照すると行の末尾にカンマが入ってしまいます。また、最終行に改行が入ってしまいます。
「行の末尾のカンマ」や「最終行に改行」を入れたくなかったので、「csv_from_result」を変更してCSVデータを取得しました。
その時に行った処理をメモします。


csv_from_result

データベースユーティリティクラスの「csv_from_result」は下記のファイルに記載されています。

system/database/DB_utility.php

クエリの結果からCSVを生成します。

    /**
     * Generate CSV from a query result object
     *
     * @access    public
     * @param    object    The query result object
     * @param    string    The delimiter - comma by default
     * @param    string    The newline character - \n by default
     * @param    string    The enclosure - double quote by default
     * @return    string
     */
    function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"')
    {
        if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
        {
            show_error('You must submit a valid result object');
        }

        $out = '';

        // First generate the headings from the table column names
        foreach ($query->list_fields() as $name)
        {
            $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;
        }

        $out = rtrim($out);
        $out .= $newline;

        // Next blast through the result array and build out the rows
        foreach ($query->result_array() as $row)
        {
            foreach ($row as $item)
            {
                $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;
            }
            $out = rtrim($out);
            $out .= $newline;
        }

        return $out;
    }

■ 変更内容

新しくhelperを作成します。
今回は「csvdata_helper.php」というヘルパーを作成、「csv_from_result」のソースをコピーし、下記のように変更しました。
行の末尾のカンマと最終行の改行を削除するように変更してます。

・ヘルパー(csvdata_helper.php)

if ( ! function_exists('csv_from_result'))
{
    function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"')
    {
        if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
        {
            show_error('You must submit a valid result object');
        }

        $out = '';

        // First generate the headings from the table column names
        foreach ($query->list_fields() as $name)
        {
            $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;
        }

        $out = rtrim($out);
        // 行の末尾のカンマを削除
        $out = rtrim($out, $delim);
        $out .= $newline;

        // Next blast through the result array and build out the rows
        foreach ($query->result_array() as $row)
        {
            foreach ($row as $item)
            {
                $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;
            }
            $out = rtrim($out);
            // 行の末尾のカンマを削除
            $out = rtrim($out, $delim);
            $out .= $newline;
        }

        // 最終行に改行を削除
        $out = rtrim($out, $newline);
        return $out;
    }
}

コントローラー側でCSVデータを取得する際に新しく作成したヘルパーの「csv_from_result」を呼び出し、CSVデータを取得します。

・コントローラー

    // 作成したヘルパーを読み込む
    $this->load->helper(array('download', 'csvdata'));

    // クエリデータ取得
    $query = $this->db->query('SQL文');

    // ヘルパーに追加した関数を呼び出し、CSVデータ取得
    $data = csv_from_result($query);

    force_download('hoge.csv', $data);

上記方法で「行の末尾のカンマ」や「最終行に改行」が入らないようにCSVデータを出力することができます。


  • このエントリーをはてなブックマークに追加

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です