我试图完成一段我已经工作了一段时间的剧本,而我却无法理解最后一部分。
目前,下面的脚本为我提供了这个输出:
除了每个用户之外,哪个是正确的'记录我只有标题行为" Mean,Median和Mode"。我想要做的是将其设置为获得4个数据列(C,F,I和L)的平均中值和模式。我有标题/标题,但在下面我需要显示配方。因此,在上图中,从MMM行开始到下一个用户的位置,MMM计算应该还有一行,但我无法弄清楚如何对其进行排序。
我可以设置平均中位数和模式公式的变量,即使PHPSpreadsheet内置了这些公式,但我不太确定如何从每个用户的数组中获取4个数据结果并为下一个计算它们行。
有什么想法吗?
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
//Create and run query
$sql = "
SELECT CONCAT(u.first_name, ' ', u.last_name) as Name,
t.ext_id as ID,
t.total_talk_time_minutes as TalkTime,
t.total_outbound as Outbound,
t.total_inbound as Inbound,
t.dealers_contacted as Dealers,
t.date_of_report as Date,
DAYNAME(t.date_of_report) as Day
FROM ambition.ambition_totals t
INNER JOIN ambition.ambition_users u
ON t.extension = u.extension
WHERE date_of_report between
curdate() - interval 5 day and curdate()
ORDER BY ID";
$result = mysqli_query($conn,$sql);
//Start the spreadsheet
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
//Create header row
$sheet->setCellValue('A1', 'Name');
$sheet->setCellValue('B1', 'User ID');
$sheet->setCellValue('C1', 'Talk Time');
$sheet->setCellValue('F1', 'Outbound');
$sheet->setCellValue('I1', 'Inbound');
$sheet->setCellValue('L1', 'Dealers');
$sheet->setCellValue('O1', 'Date');
$sheet->setCellValue('P1', 'Day');
//Preprocess the data into a multi-dimensional array
// with the id as the parent index
$resultData = array();
while($row = mysqli_fetch_assoc($result))
{
$resultData[$row['ID']][] = $row;
}
//Set starting row number
$rowNo = 1;
//Iterate over the results for each unique id
foreach($resultData as $idRecords)
{
$userFirstRow = $rowNo+1;
//Iterate over the records for this ID
foreach($idRecords as $record)
{
//Increment row number
$rowNo++;
//Add record row to spreadsheet
$sheet->setCellValue("A{$rowNo}", $record['Name']);
$sheet->setCellValue("B{$rowNo}", $record['ID']);
$sheet->setCellValue("C{$rowNo}", $record['TalkTime']);
$sheet->setCellValue("F{$rowNo}", $record['Outbound']);
$sheet->setCellValue("I{$rowNo}", $record['Inbound']);
$sheet->setCellValue("L{$rowNo}", $record['Dealers']);
$sheet->setCellValue("O{$rowNo}", $record['Date']);
$sheet->setCellValue("P{$rowNo}", $record['Day']);
}
//Increment row number
$rowNo++;
$range = 'C'.$userFirstRow.':C'.($rowNo-1);
$sheet->setCellValue("C{$rowNo}", "Mean");
$sheet->setCellValue("C{$rowNo+1}", '=AVERAGE('.$range.')');
}
$sheet->setCellValue("C{$rowNo}", $average);
var_dump($resultData);
var_dump($average);
$worksheet1 = $spreadsheet->createSheet();
$worksheet1->setTitle('Department Total');
//Add in the query for department totals
//Add results to spreadsheet
mysqli_close($conn);
$writer = new xlsx($spreadsheet);
$writer->save('Coaching Report - Test.xlsx');
?>
答案 0 :(得分:1)
作为每个用户的第一个操作,存储当前的rowid编号,以便您可以在公式范围内使用它,所以:
foreach($resultData as $idRecords)
{
$userFirstRow = $rowNo+1;
然后当你需要设置均值,中位数,模式值
替换
$sheet->setCellValue("C{$rowNo}", "Mean");
与
$range = 'C'.$userFirstRow.':C'.($rowNo-1);
$sheet->setCellValue("C{$rowNo}", "Mean");
$sheet->setCellValue("C{$rowNo+1}", '=AVERAGE('.$range.')');
和其他列的等价物
记住要做
$rowNo++;
之后以便您不会覆盖这些值