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->SetFillColor(255, 235, 255);

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);

Related Articles