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

Related Query

More Query from same tag