I have a page on my site that displays multiple html tables. One of these tables houses test values pulled from the database. The last row of this table is calculated by php, which I've been using based on the 8 tests. However, now the tests are not set at 8 so they may sometimes be 4,5 or up to 8 but there's no way to know. I have my PHP loop calculating the average by 8 but I need it to probably base the formula off of an actual count.
The PHp loop in the code below works great if there are values for all 8 tests. As you can see, it divides two row elements, stores that answer and then multiplies it by another. The math is working, I just need help getting it to count rather than using a static number of 8.
Here is the code:
//Table
$query1 = "SELECT * FROM staging";
$result1 = mysqli_query($connect,$query1);
while($row = mysqli_fetch_array($result1)){
?>//PHP loops around entire table
<table style="width:100%; border:none;
border-collapse:collapse;">
<tr style="border:none;">
<th style="border:none; text-align: left;" >Meter Test</th>
<th style="border:none;">Test 1</th>
<th style="border:none;">Test 2</th>
<th style="border:none;">Test 3</th>
<th style="border:none;">Test 4</th>
<th style="border:none;">Test 5</th>
<th style="border:none;">Test 6</th>
<th style="border:none;">Test 7</th>
<th style="border:none;">Test 8</th>
</tr>
<tr style="border: none;" >
<td style="border:none; text-align: left;">Test Rate GPM: </td>
<td><? echo $row['test1TestRateGPM'];?> </td>
<td><? echo $row['test2TestRateGPM'];?> </td>
<td><? echo $row['test3TestRateGPM'];?> </td>
<td><? echo $row['test4TestRateGPM'];?> </td>
<td><? echo $row['test5TestRateGPM'];?> </td>
<td><? echo $row['test6TestRateGPM'];?> </td>
<td><? echo $row['test7TestRateGPM'];?> </td>
<td><? echo $row['test8TestRateGPM'];?> </td>
</tr>
<tr>
<td style="border:none; text-align: left;">Meter Volume: </td>
<td><? echo $row['test1MeterVol'];?> </td>
<td><? echo $row['test2MeterVol'];?> </td>
<td><? echo $row['test3MeterVol'];?> </td>
<td><? echo $row['test4MeterVol'];?> </td>
<td><? echo $row['test5MeterVol'];?> </td>
<td><? echo $row['test6MeterVol'];?> </td>
<td><? echo $row['test7MeterVol'];?> </td>
<td><? echo $row['test8MeterVol'];?> </td>
</tr>
<tr>
<td style="border:none; text-align: left;">Tester Volume: </td>
<td><? echo $row['test1TesterVol'];?> </td>
<td><? echo $row['test2TesterVol'];?> </td>
<td><? echo $row['test3TesterVol'];?> </td>
<td><? echo $row['test4TesterVol'];?> </td>
<td><? echo $row['test5TesterVol'];?> </td>
<td><? echo $row['test6TesterVol'];?> </td>
<td><? echo $row['test7TesterVol'];?> </td>
<td><? echo $row['test8TesterVol'];?> </td>
</tr>
<tr>
<td style="border:none; text-align: left;">Tester Accuracy: </td>
<td><? echo $row['test1Accuracy'];?>% </td>
<td><? echo $row['test2Accuracy'];?>% </td>
<td><? echo $row['test3Accuracy'];?>% </td>
<td><? echo $row['test4Accuracy'];?>% </td>
<td><? echo $row['test5Accuracy'];?>% </td>
<td><? echo $row['test6Accuracy'];?>% </td>
<td><? echo $row['test7Accuracy'];?>% </td>
<td><? echo $row['test8Accuracy'];?>% </td>
<td style="border:none;">Overall</td>
</tr>
<tr>
<td style="border:none; text-align: left;">Corrected Accuracy: </td>
//Previous code only for html tables and the database values being pulled
/////////
//Following code is the loop in question
<?php
$sum=0;
for($i=1; $i<=8; $i++){
if($row["test".$i."TesterVol"] == 0){
$row["test".$i."TesterVol"] = 0.001;
}
$testFormA = $row["test".$i."MeterVol"] / $row["test".$i."TesterVol"];
$testFormB = $testFormA * $row["test".$i."Accuracy"];
$testFinalForm = $testFormB;
$sum += $testFinalForm;
?>
<td><?php echo round($testFinalForm,3) ?>%</td>
<?php }
$average = $sum / 7;
?>
<td><?php echo round($average,5)?>% </td>
</tr>
</table>
UPDATE
Screenshot of the table:

for($i=1, $n=count($row); $i<=$n; $i++){