• Terbaru

    Export Mysql ke CSV dengan PHP

    1. Form yang akan di gunakan untuk penempatan tombol export

    2. File Export. 

    .................................................

    di dalam form :


    <form action="<?php echo $base_url; ?>extoexcel2.php" method='post' id='formsearch-exportformat'>
    .
    ::: extoexcel2.php adalah file proses untuk mengeksport mysql ke csv
    tombol export di masukkan ke dalam form diatas

    Download File : <input type="submit" name="export" value="CSV Export" class="btn btn-success" />

    isi file extoexcel2.php seperti dibawah :
    <?php

    include_once "koneksi.php";
    // ====================================================================================
    $c = mysqli_query($konek, "select * from daftarkaryawan");
    if (isset($_POST['export'])) {

        $delimiter = ",";
        $filename = "exportdtkar_" . date('Y-m-d') . ".csv";

        //createfilepointer
        $f = fopen('php://memory', 'w');

        //column headers:
        $headers = array(
            'idkaryawan',
            'almat',
            'nohp'

        );
        fputcsv($f, $headers, $delimiter);

        //// Output each row of the data, format line as csv and write to file pointer
        while ($b = mysqli_fetch_assoc($c)) {
            $idkar = $b['idkaryawan'];
            $almt = $b['alamat'];
            $nohp = $b['nohp'];


            $linedata = array(
                $idkar,
                $almt,
                $nohp

            );
            fputcsv($f, $linedata, $delimiter);
        } //end of loop


        // Move back to beginning of file
        fseek($f, 0);

        // // Set headers to download file rather than displayed
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename="' . $filename . '";');

        //output all remaining data on a file pointer
        fpassthru($f);
        // exit;
    }

    .

    No comments

    Saya sangat berterimakasih apabila sahabat sekalian bersedia untuk tidak meninggalkan spam dan meninggalkan komentar yang berhubungan dengan artikel di atas.