thenorman138 Posted May 23, 2017 Share Posted May 23, 2017 Note: This site is only local and used as a calculator, so to speak, and I have no concerns about data structures or injection at this time, merely the formula in this loop. I have a working php loop involving an html table that houses test values. I am attaching a screenshot of the table, but it pulls data from a database table for 4 test attributes per 8 tests. In other words, there are 8 test columns and each column is filled with 4 test variables from the database. The 5th row calculates a percentage from a formula and after all of this is done for the 8 tests, there is an overall percentage of the 8 average values. This makes more sense looking at the screenshot, but the final row is made up from the formula. This works perfectly, except the new files we're using can have less than 8 tests, so my old formula basing the answer off of 8 tests is inaccurate now. I'm looking for a fairly quick and dirty solution for the time being that will just count how many columns actually contain data. Here is the code for the table, and the php loop data is near the bottom. Basically, I want a better way to use a count in my form loop as well as my average where they are currently using 8. <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> //here starts the loop within the table cell <?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 / 8; ?> <td><?php echo round($average,5)?>% </td> </tr> </table> Quote Link to comment Share on other sites More sharing options...
PravinS Posted May 23, 2017 Share Posted May 23, 2017 You can replace below line for($i=1; $i<=8; $i++){ by $numrows = count($row); for($i=1; $i<=$numrows; $i++) { Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 23, 2017 Share Posted May 23, 2017 I have no concerns about data structures or injection at this time You keep saying that, and you keep being wrong. If you've literally added a numbered column for each test, then you're screwed, because there's no sane way to ever get the test count again. On the other hand, you wouldn't even have that problem if you stopped abusing SQL tables as weird spreadsheets, because there would simply be one row per test and no need to count anything. So clearly the data layout does matter. And, no, making sure that input values don't screw up the query syntax doesn't suddenly become irrelevant when your application runs locally. I'm not sure why this is so hard to understand, but the reason why we use prepared statements is because we want to prevent crashes, not necessarily because we think evil attackers are after us (although that's also a factor in public web applications). Ask the people who tried to insert the name “O'Reilly” into their database and suddenly realized that none of their queries can handle that. Don't be so short-sighted. Yes, I understand you like it quick and dirty, but when you need to spend days in online forums to solve even the most trivial problems, that's not really quick, just dirty. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.