我的网站上有一个显示多个html表的页面。其中一个表包含从数据库中提取的测试值。这个表的最后一行是由php计算的,我一直在使用8次测试。但是,现在测试没有设置为8,所以它们有时可能是4.5或者最多8但是没有办法知道。我有我的PHP循环计算平均值8但我需要它可能基于实际计数的公式。
如果所有8个测试都有值,则下面代码中的PHp循环效果很好。正如您所看到的,它将两个行元素分开,即回答然后将其乘以另一个的行。数学运算正常,我只需要帮助计算它而不是使用静态数字8。
以下是代码:
//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>
更新
表格截图:
答案 0 :(得分:1)
好的,我有一个解决方案。
<?php
// Import existing data structure
$data = [
// Test Rate GPM
test1TestRateGPM => 0.5,
test2TestRateGPM => 5,
test3TestRateGPM => 10,
test4TestRateGPM => 15,
test5TestRateGPM => 25,
test6TestRateGPM => 100,
test7TestRateGPM => 150,
test8TestRateGPM => 0,
// Meter Volume
test1MeterVol => 0,
test2MeterVol => 0,
test3MeterVol => 0,
test4MeterVol => 2.74,
test5MeterVol => 4.95,
test6MeterVol => 15.7,
test7MeterVol => 33.5,
test8MeterVol => 0,
// Tester Volume
test1TesterVol => 0.5,
test2TesterVol => 1,
test3TesterVol => 2,
test4TesterVol => 4.04,
test5TesterVol => 6.7,
test6TesterVol => 20,
test7TesterVol => 40.1,
test8TesterVol => 0,
// Tester Accuracy
test1Accuracy => 1,
test2Accuracy => 1,
test3Accuracy => 1,
test4Accuracy => 1,
test5Accuracy => 1,
test6Accuracy => 1,
test7Accuracy => 1,
test8Accuracy => 0
];
// Make data structure sane...
// One row per test. Each row to contain the relevant fields
// Figure out the lowest and highest indices based on the key names, and the keys we want
$lowIndex = INF;
$highIndex = -INF;
$keys = [];
foreach ($data as $key => $value) {
$matches = [];
if (preg_match('/^test([0-9]+)(.*)$/', $key, $matches)) {
$index = (int) $matches[1];
$lowIndex = min($lowIndex, $index);
$highIndex = max($highIndex, $index);
$keys[] = $matches[2];
}
}
$keys = array_unique($keys);
// Loop through to fix the data
$tests = [];
for ($i = $lowIndex; $i <= $highIndex; $i++) { // Assumes contigous indices!
$test = (object)[];
foreach ($keys as $key) {
$test->{$key} = $data['test' . $i . $key];
}
if ($test->TesterVol) {
$test->CorrectedAccuracy = $test->MeterVol / $test->TesterVol;
}
$tests[] = $test;
}
// Yeah! Now we have some sane data. For example, if you use print_r($tests)...
// Array
// (
// [0] => stdClass Object
// (
// [TestRateGPM] => 0.5
// [MeterVol] => 0
// [TesterVol] => 0.5
// [Accuracy] => 0.5
// [CorrectedAccuracy] => 0
// )
//
// [1] => stdClass Object
// (
// [TestRateGPM] => 5
// [MeterVol] => 0
// [TesterVol] => 1
// [Accuracy] => 5
// [CorrectedAccuracy] => 0
// )
// ...
// Output the table!
echo '<table>';
// Header Row
echo '<thead><tr>';
echo '<th>Test #</th>';
foreach ($keys as $key) {
echo '<th>', htmlspecialchars($key), '</th>';
}
echo '</tr></thead>';
// Main data
echo '<tbody>';
foreach ($tests as $testIndex => $test) {
echo '<tr>';
echo '<td>', htmlspecialchars($testIndex), '</td>';
foreach ($keys as $key) { // Using this original array of keys ensures we stay in-order
echo '<td>', htmlspecialchars($test->{$key}), '</td>';
}
echo '</tr>';
}
echo '<tr><td>Average</td>', str_repeat('<td> </td>', count($keys) - 1), '<td>';
echo array_sum(array_map(function ($test) {
return $test->CorrectedAccuracy;
}, $tests)) / count($tests);
echo '</td></tr>';
echo '</tbody>';
echo '</table>';
更正数据库架构。一旦你完成了这个,这就简单得多了。
此代码重新处理现有架构,以便每个测试都是自己的记录。一旦采用该格式,您就可以使用map / reduce等所有标准机制来处理数据。你甚至可以在MySQL中完成所有这些工作。
需要注意的其他事项......始终在HTML上下文中输出的任意数据周围使用htmlspecialchars()
。你今天正在处理数字,当然,但有一天你可能不会这样做,输出的代码应该正确处理。
同样在您的某个修改中,您在查询中直接使用$_REQUEST
数据,未经过规范化或转义或其他任何内容。切换到参数化查询以避免严重的安全问题。您的数据库很可能已被劫持,因为机器人总是在互联网上寻找像您这样的网站来利用它。
如果你真的想让数据走向错误的方向,我会把它作为练习留给读者。 :-)但是,如果你有问题,请发表评论。