This is my I think it will be helpful for you.
first of all, you can create an export button
View:
<a style="position: relative; top: -20px;" href="<?php echo base_url(); ?>Ap/Export1" class="btn btn-info" role="button">Export</a>
Controller:
public function Export1(){ // Filter the excel data function filterData(&$str){ $str = preg_replace("/t/", "\t", $str); $str = preg_replace("/r?n/", "\n", $str); if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; } // Excel file name for download $fileName = "members-data_" . date('Y-m-d') . ".xls"; // Column names $fields = array('id', 'email', 'LAST NAME'); // Display column names as first row $excelData = implode("t", array_values($fields)) . "n"; // Fetch records from database $query = $this->db->query("SELECT * FROM users where role in ('admin','superadmin') ORDER BY id ASC")->result_array(); if(sizeof($query) > 0){ // Output each row of the data foreach($query as $row){ $lineData = array($row['id'], $row['email'], $row['username']); array_walk($lineData, 'filterData'); $excelData .= implode("t", array_values($lineData)) . "n"; } }else{ $excelData .= 'No records found...'. "n"; } // Headers for download header("Content-Type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename="$fileName""); // Render excel data echo $excelData; }
Question and answer is powered by AnsPress.io