I want to export data to excel in CI. But i have issue
JavaScript
x
Undefined index: wc_order_id
If I
JavaScript
print_r(array_values($data)); exit;
the value is correct, only I cannot export to Excel.
This my controller:
JavaScript
public function createExcel($awals, $akhirs, $status) {
$fileName = 'Reservation.xlsx';
$data['data'] = $this->M_reservation->all_order($awals, $akhirs, $status) ;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$total = 0;
$sheet->setCellValue('A1', 'No');
$sheet->setCellValue('B1', 'Booking ID');
$sheet->setCellValue('C1', 'Order Date');
$sheet->setCellValue('D1', 'Client Name');
$sheet->setCellValue('E1', 'Client Email');
$sheet->setCellValue('F1', 'Phone');
$sheet->setCellValue('G1', 'Inclution');
$sheet->setCellValue('H1', 'Room name');
$sheet->setCellValue('I1', 'Bed Order');
$sheet->setCellValue('J1', 'Status');
$sheet->setCellValue('K1', 'Total Order');
$rows = 2;
$no = 1;
$total=0;
foreach ($data as $val){
$sheet->setCellValue('A' . $rows, $no++);
$sheet->setCellValue('B' . $rows, $val['wc_order_id']);
$sheet->setCellValue('C' . $rows, $val['post_date']);
$sheet->setCellValue('D' . $rows, $val['display_name']);
$sheet->setCellValue('E' . $rows, $val['user_email']);
$sheet->setCellValue('F' . $rows, $val['meta_value']);
$sheet->setCellValue('G' . $rows, $val['display_name']);
$sheet->setCellValue('H' . $rows, $val['post_title']);
$sheet->setCellValue('I' . $rows, $val['room_num_search']);
$sheet->setCellValue('J' . $rows, $val['status']);
$sheet->setCellValue('K' . $rows, $val['total_order']);
$total = $total+$val['total_order'];
$rows++;
}
$sheet->setCellValue('B'.$rows , "Total");
$sheet->setCellValue('K'.$rows , $total);
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Reservation.xlsx"');
header('Cache-Control: max-age=0');
ob_clean();
$writer->save('php://output');
}
Advertisement
Answer
You’re storing the value in $data['data']
which means the variable $data
has a key
named data
that stores the resultant array. So, you can either
loop through
$data['data']
JavaScript$data['data'] = $this->M_reservation->all_order($awals, $akhirs, $status) ; // no change
// ...
// ...
foreach($data['data'] as $val){ // change here
// your code here
}
or store resultant array in
$data
JavaScript$data = $this->M_reservation->all_order($awals, $akhirs, $status) ; // change here
// ...
// ...
foreach($data as $val){ // no change
// your code here
}
See if it helps you.