-
Tajar_Dobro
Junior Member
-
Posts: 20
Threads: 10
Joined: May 2021
Reputation:
0
Hi all
How can i export to excel the output from search result?
I have done the export but not the total rows only the results of search.
-
InsiteFX
Super Moderator
-
Posts: 6,575
Threads: 331
Joined: Oct 2014
Reputation:
240
-
Tajar_Dobro
Junior Member
-
Posts: 20
Threads: 10
Joined: May 2021
Reputation:
0
Hi, the project is in CI 3
-
InsiteFX
Super Moderator
-
Posts: 6,575
Threads: 331
Joined: Oct 2014
Reputation:
240
-
demyr
Senior Member
-
Posts: 330
Threads: 17
Joined: Sep 2018
Reputation:
7
Hi. First you need to retrieve all data then, it is a model work. Then you need a foreach.
I use PhpSpreadSheet to export excel, which seems to be the best for now and let me share a copy of my codes with you:
PHP Code: function download_invoice($user_id){
$spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $date = date('Y-m-d');
/*set column names*/ $table_columns = array("Company Name", "Address", "Email", "phone", "Invoice Date", "Product Name", "etc", "etc"); $column = 1; foreach ($table_columns as $field) { $sheet->setCellValueByColumnAndRow($column, 1, $field); $column++; } /*end set column names*/
$invoice_data = $this->InvoiceModel->download_invoice($user_id); //get your data from model
$excel_row = 2; //now from row 2
foreach ($invoice_data as $row) { $sheet->setCellValueByColumnAndRow(1, $excel_row, $row->company_name); $sheet->setCellValueByColumnAndRow(2, $excel_row, $row->user_address.'-'.$row->user_country); $sheet->setCellValueByColumnAndRow(3, $excel_row, $row->user_email); $sheet->setCellValueByColumnAndRow(4, $excel_row, $row->uye_phone); $sheet->setCellValueByColumnAndRow(5, $excel_row, $row->invoice_date); $sheet->setCellValueByColumnAndRow(6, $excel_row, $row->product_date); $sheet->setCellValueByColumnAndRow(7, $excel_row, $row->etc);
$excel_row++; }
$invoice_name = 'Invoice-'.$user_id.'-'.$date.'.xls'; $object_writer = new Xlsx($spreadsheet); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$invoice_name.'"'); $object_writer->save('php://output');
}
-
Tajar_Dobro
Junior Member
-
Posts: 20
Threads: 10
Joined: May 2021
Reputation:
0
Well, I have the main query in the DB.
now I want only to export the return from the search. also, PHP excel does not work.
This code exports me all the values whereas I need only the result from the search
//Expor to Excel
public function export_csv(){
$filename = 'expenses_'.date('Y-m-d').'.csv';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/csv; ");
$expenses_data = $this->expenses_model->export_expenses();
$file = fopen('php://output', 'w');
$header = array("Nr.",
"Title",
"Date",
"Time",
"User",
"Amount",
);
fputcsv($file, $header);
foreach ($expenses_data as $key=>$line){
fputcsv($file,$line);
}
fclose($file);
exit;
}
-
suvi
Newbie
-
Posts: 9
Threads: 0
Joined: Nov 2020
Reputation:
0
-
paulkd
Member
-
Posts: 75
Threads: 17
Joined: Oct 2015
Reputation:
0
(06-14-2021, 12:35 AM)Tajar_Dobro Wrote: Well, I have the main query in the DB.
now I want only to export the return from the search. also, PHP excel does not work.
This code exports me all the values whereas I need only the result from the search
//Expor to Excel
public function export_csv(){
$filename = 'expenses_'.date('Y-m-d').'.csv';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/csv; ");
$expenses_data = $this->expenses_model->export_expenses();
$file = fopen('php://output', 'w');
$header = array("Nr.",
"Title",
"Date",
"Time",
"User",
"Amount",
);
fputcsv($file, $header);
foreach ($expenses_data as $key=>$line){
fputcsv($file,$line);
}
fclose($file);
exit;
}
Can you show us the search code ?
-
Tajar_Dobro
Junior Member
-
Posts: 20
Threads: 10
Joined: May 2021
Reputation:
0
(06-16-2021, 05:12 AM)Tajar_Dobro Wrote: PHP Code: public function search_expense($search=NULL){
$keyword = $this->input->get('search',TRUE); // pagination settings $total_rows = $this->db->select('*')->from('expenses')->like("expense",$keyword)->get()->num_rows(); $limit = 15; $config["base_url"] = base_url("expenses/search"); $config["total_rows"] = $total_rows; $config["per_page"] = $limit;
$config['next_link'] = '→'; $config['prev_link'] = '←'; $config['full_tag_open'] = "<ul class='pagination justify-content-center'>"; $config['full_tag_close'] = "</ul>"; $config['num_tag_open'] = '<li class="page-item">'; $config['num_tag_close'] = '</li>'; $config['cur_tag_open'] = '<li class="page-item active"><a class="page-link">'; $config['cur_tag_close'] = '</a></li>'; $config['next_tag_open'] = '<li class="page-item">'; $config['next_tagl_close'] = '</a></li>'; $config['prev_tag_open'] = '<li class="page-item">'; $config['prev_tagl_close'] = '</li>'; $config['first_tag_open'] = "<li class='page-item disabled'>"; $config['first_tagl_close'] = "</a></li>"; $config['last_tag_open'] = '<li class="page-item">'; $config['last_tagl_close'] = '</a></li>'; $config['attributes'] = array('class' => 'page-link');
$this->pagination->initialize($config); $header["links"] = $this->pagination->create_links();
$start = $this->uri->segment(3); $header['query'] = $this->db->select("*") ->from("expenses") ->like("title",$keyword) ->or_like("user",$keyword) ->or_like("amount",$keyword) ->limit($limit,$start) ->order_by('id','DESC') ->get() ->result_array();
$this->load->view('expenses/index', $header);
}
paulkd
(06-14-2021, 12:35 AM)Tajar_Dobro Wrote: Well, I have the main query in the DB.
now I want only to export the return from the search. also, PHP excel does not work.
This code exports me all the values whereas I need only the result from the search
//Expor to Excel
public function export_csv(){
$filename = 'expenses_'.date('Y-m-d').'.csv';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/csv; ");
$expenses_data = $this->expenses_model->export_expenses();
$file = fopen('php://output', 'w');
$header = array("Nr.",
"Title",
"Date",
"Time",
"User",
"Amount",
);
fputcsv($file, $header);
foreach ($expenses_data as $key=>$line){
fputcsv($file,$line);
}
fclose($file);
exit;
}
Can you show us the search code ?
|