I'm trying to export a simple table containing only two fields id
, email
to a CSV file using csv_from_result()
in CodeIgniter.
The first line of the generated CSV file contains the name of the column, but I only want the the data.
Is there any way to skip this fist line?
This is my code :
$query = $this->EE->db->query("SELECT email FROM exp_sct_mailing_list");
$this->EE->load->dbutil();
$data = $this->EE->dbutil->csv_from_result( $query, ",", "\r\n" );
$this->EE->load->helper('download');
force_download("mailing_list.csv", $data);
exit;
The easiest way to do this is to just remove the first line like so:
$query = $this->EE->db->query("SELECT email FROM exp_sct_mailing_list");
$this->EE->load->dbutil();
$data = ltrim(strstr($this->EE->dbutil->csv_from_result($query, ',', "\r\n"), "\r\n"));
$this->EE->load->helper('download');
force_download("mailing_list.csv", $data);
exit;
Here we extract only the contents after from the first CRLF \r\n
to the end of the data. Then we left trim the CRLF out and thus have removed the first line.