I have a function that generates excel file from my data like screenshot below
There are 2 sets of issues here (yellow and blue)
Yellow: The columns should start from row #2 where it has 1
as A2
value
Blue: My code somehow adds extra line for each data object which was 1
at first then became 2
and so on… (so for each set of data it adds extra 2 empty rows)
Code
here is my loop part that is in charge of printing data as you see in screenshot above:
$no = 2;
foreach ($br as $key => $value) {
$no = $no + $key;
$sheet->setCellValue('A'.$no, $key+1);
$sheet->setCellValue('B'.$no, $value->va_number);
$sheet->setCellValue('C'.$no, $value->customer_name);
$sheet->setCellValue('D'.$no, $value->cabang);
$sheet->setCellValue('E'.$no, $value->payment_date);
$sheet->setCellValue('F'.$no, $value->payment_amount);
$sheet->setCellValue('G'.$no, $value->br_no);
$dtl = DB::table('bank_receive_dtl')
->join('inv','inv.id','=','bank_receive_dtl.inv_id')
->where('bank_receive_dtl.br_id','=',$value->br_id)
->select('bank_receive_dtl.*','inv.inv_no')
->get();
foreach ($dtl as $key => $item) {
$no ++;
$sheet->setCellValue('H'.$no, $item->inv_no);
$sheet->setCellValue('I'.$no, $item->payment_amount);
}
$no--;
}
The problem caused by $no ++;
and $no--;
in second loop where some of my objects have multiple invoice numbers as you see.
PS: Here is what ideal final result should be look like
except coloring part :)
Can you point me to the part that I’ve made mistake?
Update
Yellow issue is solved by moving $no ++
at the bottom like this:
foreach ($dtl as $index => $item) {
$sheet->setCellValue('H'.$no, $item->inv_no);
$sheet->setCellValue('I'.$no, $item->payment_amount);
$no ++; // <--moved down here
}
Blue issue still remains.