2

I need to export results from a MySQL query into a csv file in codeigniter.

This is the model:

public function export_csv()
{

       $this->load->dbutil();
    $this->load->helper('file');
    $this->load->helper('download');
    $delimiter = ",";
    $newline = "\r\n";
    $file_name = 'BVN_REPORTS'.date("Y-m-d h-i-s").'.csv';
    $query = 'SELECT  account_name as "Account Name",
    api_account_name as "Verified Name", 
    account_num "Account Number", 
    bvn "Bank Verification Number (BVN)", bank_name as "BANK NAME" 
    from ew_employees where bvn is not null
    ORDER BY bank_name ';
   $result = $this->db->query($query);
   $data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
   if(force_download($filename, $data)){
   echo 'Done';
   }
   else {echo 'Not Done';}
    }

And this is the controller:

  public function get_csv()
{
     $this->load->model('employees_model');
     $this->employees_model->export_csv();
}

The result is always 'Not Done'...how can i force a csv download of the query results.Thanks

5
  • Are you sure file is loaded ?? Commented Apr 12, 2016 at 11:58
  • The file is supposed to be created dynamically and populated with the query results Commented Apr 12, 2016 at 12:02
  • Check manually file is created or not Commented Apr 12, 2016 at 12:07
  • The code is supposed to create the file...it is not in existence yet Commented Apr 12, 2016 at 12:16
  • check file permissions Commented Apr 12, 2016 at 12:17

3 Answers 3

2

According to the Question title let me Answer the question.Generally it may help other when visit here. what i generally do when exporting data to csv.

function data_to_csv($data, $headers = TRUE, $filename= "")
{
    if ( ! is_array($data))
    {
        show_error('invalid Data provided');
    }

    $array = array();

    if ($headers)
    {
        $array[] = array_keys($data[0]);
    }
    foreach ($data as $row)
    {
        $line = array();
        foreach ($row as $item)
        {
            $line[] = $item;
        }
        $array[] = $line;
    }
        header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=\"$filename".".csv\"");
        header("Pragma: no-cache");
        header("Expires: 0");

        $handle = fopen('php://output', 'w');

        foreach ($array as $array) {
            fputcsv($handle, $array);
        }
            fclose($handle);
        exit;
}
Sign up to request clarification or add additional context in comments.

3 Comments

How can i export multiple tables data into one csv file ?
It depend on your requirement one cannot say anything without knowing the exact requirements
Basically i want to export data of two html table in csv file one below other
0

controller

    $result = $this->registration_model->exportToCsv();
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=\"application".".csv\"");
    header("Pragma: no-cache");
    header("Expires: 0");
    $handle = fopen('php://output', 'w');
   fputcsv($handle, array('Sr No.', 'Apply for', 'Name', 'mobile', 'email_id', 'work_experinece', 'pan', 'refrence', 'pro_img1', 'pro_img2', 'created_at'));
                    $i = 1;
                    foreach ($result as $data) {
                        fputcsv($handle, array($i, $data["apply_for"], $data["full_name"], $data["mobile"], $data["email_id"], $data["work_experinece"], $data["pan"], $data["refrence"], $data["pro_img1"], $data["pro_img2"], $data["created_at"]));
                        $i++;
                    }
                        fclose($handle);
                    exit;

Model

$this->db->select("apply_for,full_name,mobile,address,email_id,work_experinece,pan,refrence,pro_img1,pro_img2,created_at");
$this->db->order_by("app_id", "DESC");
$this->db->from("tablename"); 
$query = $this->db->get();
return $query->result_array();

Comments

0

I added a line to Abdul Manan's answer to deal with UTF-8 characters :

       foreach ($array as $array) {
        fputs($handle, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
        fputcsv($handle, $array);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.