平成30/ 2018-10-23 22:00
PHP/ CSVダウンロード

//ダウンロード処理
if(
isset($_POST["download_btn_x"])
) {
    header("Cache-Control: public");
    header("Pragma: public");
    header("Content-Disposition: attachment;filename=\"analyze.csv\"");
    header("Content-Type: application/octet-stream");
    
    $csv = array();
    $csv[] = "ID";
    $csv[] = "URL";
    $csv[] = "キーワード";

    $csv[] = "DOCOMO公式";
    $csv[] = "TITLE";
    $csv[] = "URL";

    $csv[] = "DOCOMO(Goo)";
    $csv[] = "TITLE";
    $csv[] = "URL";

    $csv[] = "DOCOMO(Yahoo)";
    $csv[] = "TITLE";
    $csv[] = "URL";

    $csv[] = "Softbank(Yahoo)";
    $csv[] = "TITLE";
    $csv[] = "URL";

    $csv[] = "Google";
    $csv[] = "TITLE";
    $csv[] = "URL";

    $csv[] = "CSVアップロード日時";
    foreach ($csv as $key => $val) {
        $csv[$key] = csvFormat($val);
    }
    echo mb_convert_encoding(implode(",", $csv), "sjis-win", "utf8"). "\r\n";
    
    foreach ($s["search_data"] as $key => $val) {
        $csv = array();
        $csv[] = csvFormat($val["csv_num"]);
        $csv[] = csvFormat($val["url"]);
        $csv[] = csvFormat($val["keyword"]);
        $csv[] = csvFormat($val["docomo_ranking"]);
        $csv[] = csvFormat($val["docomo_title"]);
        $csv[] = csvFormat($val["docomo_url"]);

        $csv[] = csvFormat($val["goo_ranking"]);
        $csv[] = csvFormat($val["goo_title"]);
        $csv[] = csvFormat($val["goo_url"]);

        $csv[] = csvFormat($val["yahoo_i_ranking"]);
        $csv[] = csvFormat($val["yahoo_i_title"]);
        $csv[] = csvFormat($val["yahoo_i_url"]);

        $csv[] = csvFormat($val["yahoo_ranking"]);
        $csv[] = csvFormat($val["yahoo_title"]);
        $csv[] = csvFormat($val["yahoo_url"]);

        $csv[] = csvFormat($val["google_ranking"]);
        $csv[] = csvFormat($val["google_title"]);
        $csv[] = csvFormat($val["google_url"]);
        $csv[] = csvFormat($val["google_terminal_mark"]);

        $csv[] = csvFormat($val["uploaded_at"]);
        echo mb_convert_encoding(implode(",", $csv), "sjis-win", "utf8"). "\r\n";
    }
    exit;
}