$stmt = mysqli_query($connection, $sql);
if($stmt === false) {
die( print_r( mysqli_errors(), true) );
}
$category1 = 0;
$category2 = 0;
$category3 = 0;
$category4 = 0;
$category5 = 0;
$category6 = 0;
$category7 = 0;
$category8 = 0;
while($row = mysqli_fetch_array($stmt)) {
if(strcasecmp($row['fruit_name'], 'apple') == 0) {
$category1++;
}
if(strcasecmp($row['fruit_name'], 'banana') == 0) {
$category2++;
}
if(strcasecmp($row['fruit_name'], 'cherry') == 0) {
$category3++;
}
if(strcasecmp($row['fruit_name'], 'dragon fruit') == 0) {
$category4++;
}
if(strcasecmp($row['fruit_name'], 'eastern hawthorn') == 0) {
$category5++;
}
if(strcasecmp($row['fruit_name'], 'finger lime') == 0) {
$category6++;
}
$sum = $row['total_fruit'] + $sum;
$output .= '<tr>
<td>' . $row['fruit_name'] . '</td>
<td align=center>' .$row['total_fruit'] . '</td>
</tr>';
}
$output .= '<tr><td>$sum</td></tr></table>';
echo $output;
From the code above, I have calculated the sum of all rows(sum of fruits) and display it outside the while loop. I have 6 different fruits name that needs to be counted right after the last row of each same fruit. For example:
Fruit name Total Fruit
________________________
Apple 1
Apple 1
Apple 1
------------------------
Subtotal 3
------------------------
Banana 1
Banana 1
------------------------
Subtotal 2
------------------------
.
.
.
Total $sum
What I want to do is to display the subtotal of the same elements in the last row of each category