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 Query

More Query from same tag