I have a php script that creates an excel file with 2 worksheets. The first sheet is perfect because it's formulating values for records belonging to individual users, so I've used an associative array using their user Id as the index.
The 2nd sheet just gets department totals for 5 days so instead of saying "for each ID create a record array and formulate on that record" I want to say "load all 5 rows as one record group in the array so I can formulate based on only those records". I hope that makes sense.
The way my script is now, it's putting my formulation row (mean, median and mode) between all 5 records, but I only need one formulation row after all 5 records. Basically I'm just looping too much right now and I'm not sure where to refactor it.
Here's the script:
$resultData2 = array();
while($row2 = mysqli_fetch_assoc($result2))
{
$resultData2[$row2['TalkTime']][] = $row2;
}
//Set starting row number
$rowNo2 = 1;
// //Iterate over the results for each unique id
foreach($resultData2 as $idRecords2)
{
$userFirstRow2 = $rowNo2+1;
//Iterate over the records for this ID
foreach($idRecords2 as $record2)
{
//Increment row number
$rowNo2++;
//Add record row to spreadsheet
$worksheet1->setCellValue("A{$rowNo2}", $record2['TalkTime']);
$worksheet1->setCellValue("D{$rowNo2}", $record2['Outbound']);
$worksheet1->setCellValue("G{$rowNo2}", $record2['Inbound']);
$worksheet1->setCellValue("J{$rowNo2}", $record2['Dealers']);
$worksheet1->setCellValue("M{$rowNo2}", $record2['Date']);
$worksheet1->setCellValue("N{$rowNo2}", $record2['Day']);
}
//Increment row number
$rowNo2++;
$worksheet1->setCellValue("A{$rowNo2}", "Mean");
$worksheet1->setCellValue("B{$rowNo2}", "Median");
$worksheet1->setCellValue("C{$rowNo2}", "Mode");
$worksheet1->setCellValue("D{$rowNo2}", "Mean");
$worksheet1->setCellValue("E{$rowNo2}", "Median");
$worksheet1->setCellValue("F{$rowNo2}", "Mode");
$worksheet1->setCellValue("G{$rowNo2}", "Mean");
$worksheet1->setCellValue("H{$rowNo2}", "Median");
$worksheet1->setCellValue("I{$rowNo2}", "Mode");
$worksheet1->setCellValue("J{$rowNo2}", "Mean");
$worksheet1->setCellValue("K{$rowNo2}", "Median");
$worksheet1->setCellValue("L{$rowNo2}", "Mode");
$rowNo++;
}