score:1

After looking at the image again, I can understand your problem. You basically need to prepare your data, instead of printing the PDF directly.

You could use a map to calculate your output like so:

``````\$total = array("freq" => 0, "menit" => 0, "jam" => 0);
\$groups = array();
while (\$data = mysqli_fetch_array(\$query)) {
\$names = explode(",",\$data['nama_tmp']);
foreach (\$names as \$name) {
if (!array_key_exists(\$name, \$groups)) {
\$groups[\$name] = array("freq" => 0, "menit" => 0, "jam" => 0);
}
\$groups[\$name]["freq"] += \$data["freq"];
\$groups[\$name]["menit"] += \$data["menit"];
\$groups[\$name]["jam"] += \$data["jam"];
\$total["freq"] += \$data["freq"];
\$total["menit"] += \$data["menit"];
\$total["jam"] += \$data["jam"];
}
}
``````

Now you should have all your data in \$groups and you can generate your PDF from that.

``````foreach (\$groups as \$name => \$group) {
\$pdf->Ln();
\$pdf->Cell(90,5,\$name,1,0,'L',0);
\$pdf->Cell(20,5,\$group['freq'],1,0,'C',0);
\$pdf->Cell(25,5,\$group['menit'],1,0,'C',0);
\$pdf->Cell(25,5,\$group['jam'],1,0,'C',0);
}

\$pdf->Ln(5);
\$pdf->SetFillColor(255, 235, 255);
\$pdf->SetFont('times','B',8);
\$pdf->Cell(90,5,'TOTAL',1,0,'C',1);
\$pdf->Cell(20,5,\$total["freq"],1,0,'C',1);
\$pdf->Cell(25,5,\$total["menit"],1,0,'C',1);
\$pdf->Cell(25,5,\$total["jam"],1,0,'C',1);
``````

Note: Instead of using a variable for every column sum, I have used a \$total map array instead. This makes it more readable. Also you should consider using some functions for each part of your script.

The main function could then be as simple as this:

``````\$data = readData();
\$groups= calculateSums(\$data);
\$pdf = generatePDF(\$groups);
``````