Skip First Line In csv_from_result() In CodeIgniter Skip First Line In csv_from_result() In CodeIgniter codeigniter codeigniter

Skip First Line In csv_from_result() In CodeIgniter


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.


Saddly there is no way to do pass a parameter to function csv_from_result and avoid the column names, but you can build your custom csv_from_result function based on the code of the original function an removing the undesired part:

/*** Generate CSV from a query result object** @param object $query Query result object* @param string $delim Delimiter (default: ,)* @param string $newline Newline character (default: \n)* @param string $enclosure Enclosure (default: ")* @return string*/function my_custom_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 = '';// Blast through the result array and build out the rowswhile ($row = $query->unbuffered_row('array')){foreach ($row as $item){$out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;}$out = substr(rtrim($out), 0, -strlen($delim)).$newline;}return $out;}

The code is based on the implementation of csv_from_result taken from here: https://github.com/EllisLab/CodeIgniter/blob/develop/system/database/DB_utility.php


You could use array_shift

$data = array_values(array_shift($data)); 

This will remove the first row.

your code becomes:

$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" );$data = array_values(array_shift($data)); //skip the first line$this->EE->load->helper('download');force_download("mailing_list.csv", $data);exit;