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);
Source: stackoverflow.com
Related Articles
- how to COUNT explode imploding data with comma?
- Highcharts - Global configuration with common code and unique data & Headings
- Data with timestamp group to month(like Jan, Feb, March) and showing as total count for month in Highcharts
- Highstock, True way of Get count of shown points after setExtreme (Zooming) - WITHOUT counting all data with MIN and MAX
- Have an issue with JavaScript, AJAX code displaying data
- Highcharts series visibility with csv data source
- Format number data with comma and unit in Highchart
- How to have multiple highcharts with different series data in vuejs without repeating code
- Highcharts cloud issue with data source when duplicating chart
- Setting Additional Data to highcharts tooltip with formatter doesn't work if points count exceed threshold
- How to change value with count data
- Reload chart data via JSON with Highcharts
- Resize data points with highcharts
- Highcharts doesn't display series with lots of data points
- Highcharts data series issue with ajax/json and PHP
- creating highchart with ajax json data
- Highcharts - Dyanmic graph with no initial data
- Load data into Highcharts with Ajax
- Plot Highchart Gauge with JSON Data
- HighChart Heatmap with JSON data
- Highcharts custom visuals with Data binding PowerBI
- How to display highchart y axis with constistant data
- Updating data in charts, highcharts with angular, highcharts-ng
- Highchart data series filled with different colors
- Highcharts with JSON data and multiple series
- Show gap of missing data with Highstock
- Highcharts scatter plot with lots of data points running really slow
- Appending Data with Highcharts (real time updating chart)
- Create Density Heatmap Chart using Jquery with array of data
- highcharts with null data points
- Calculating a min y axis value in highcharts
- Highcharts : hiding bottom-most x axis grid line
- Highcharts: min value of y axis on the bottom of chart
- Hightlight x-axis label in Highcharts
- Highcharts: showing tooltip with calculated value at Crosshair
- Highchart multicolour areaspline
- Highcharts data set array
- Highchart download image is changing aspect ratio
- Highcharts - Suppress points being dragged on drag event
- Highcharts - mouse event in chart - e.Offset solution across browsers
- Change SVG stroke options in Highcharts
- Highcharts pie legend clipped off
- Structuring data to be sent to HighCharts in view
- highcharts - windrose stackLabel doesn't work properly
- Highcharts yaxis price line
- Dynamically add and remove custom export button in highcharts
- Highcharts line and points are shown only on mouse hover
- Save and load highcharter graph in R
- How to display HighCharts with tables
- Adding Date Labels to a HighCharts line graph