Jump to content

Preforming PHP calculations with table row data


Recommended Posts

I have an html table with some numerical values in each data row. There are 8 columns representing 8 tests. At the bottom of each column is a corrected accuracy cell which represents a math formula from the three rows above it. Basically, it divides the meter volume by the tester volume, multiplies that result by the tester accuracy, and divides that result by 100. I have attempted something with the following code:

 

<table style="width:100%; border:none;
    border-collapse:collapse;">
    <? php
 
    $test1FormA = $row['test1MeterVol'] / $row['test1TesterVol'];
    $test1FormB = $test1FormA * $row['test1Accuracy'];
    $test1FinalForm = $test1FormB / 100;
 
    ?>
 
<tr>
<td style="border:none; text-align: left;">Meter Volume: </td>
<td><? echo $row['test1MeterVol'];?> </td>
</tr>
<tr>
<td style="border:none; text-align: left;">Tester Volume: </td>
<td><? echo $row['test1TesterVol'];?> </td>
</tr>
<tr>
<td style="border:none; text-align: left;">Tester Accuracy: </td>
<td><? echo $row['test1Accuracy'];?> </td>
</tr>
<tr>
<td style="border:none; text-align: left;">Corrected Accuracy: </td>
<td><? echo $test1FinalForm;?> </td>
 
</tr>

 

</table>
 
However, my page no longer loads with this so I'm assuming I'm doing something wrong with my variables in the top, maybe with syntax.
 
I just need it to take the values for those three data rows and use them in the formulas so that I can put the final result in the corrected accuracy field. 
Edited by thenorman138
Link to post
Share on other sites

Good practice dictates that you should enable php error checking while you are in development mode.  See my signature.

 

ps - one does not store a result from an echo statement.

Edited by ginerjm
Link to post
Share on other sites

I will enable error checking now for sure. And I do realize, but I actually have all of those echo statements coming from a full SQL query at the head of my code.

 

Also, benanamen, I just edited my code to remove that. I'm currently trying without that echo but still no luck.

Edited by thenorman138
Link to post
Share on other sites

...but I actually have all of those echo statements coming from a full SQL query at the head of my code.

 

What does that statement mean?  You are assigning an echo to a php variable? That would be an error.

 

And this code is a little easier to type in when you don't have to keep using php tags:

$test1FormA = $row['test1MeterVol'] / $row['test1TesterVol'];
$test1FormB = $test1FormA * $row['test1Accuracy'];
$test1FinalForm = $test1FormB / 100;
$code=<<<heredocs
<table style='width:100%;' border=0>
<tr>
<td style="text-align: left;">Meter Volume: </td>
<td>{$row['test1MeterVol']}</td>
</tr>
<tr>
<td style="text-align: left;">Tester Volume: </td>
<td>{$row['test1TesterVol']}</td>
</tr>
<tr>
<td style="text-align: left;">Tester Accuracy: </td>
<td>{$row['test1Accuracy']}</td>
</tr>
<tr>
<td style="text-align: left;">Corrected Accuracy: </td>
<td>$test1FinalForm/td>
</tr>
</table>
heredocs;
   echo $code;

Using 'heredocs' allow you to type html code with php vars mixed in very easily.  Read up on it in the manual

Your table says you don't want a border.  So - why do you feel like using border:none in all of your td tags as well as a border-collapse?

Edited by ginerjm
Link to post
Share on other sites

I removed the echo from my variable section. I'm only saying that the echo statements in my table data rows are echoing database variables. They show up in my html table cells, I"m just trying to use those values to preform the calculations. 

Link to post
Share on other sites

First off, give your variables meaningful names rather than 'test1FormA', 'test1FormB', etc.

 

Second, based on the database field names (i.e. 'test1'), I would assume you have columns for each test. That is not the proper way to store that type of data. Each test should be an individual record. If the tests need to be associated (say with an execution event) then you would have separate tables for the even and one for the tests.

 

Third, rather than create logic in PHP to calculate the value for each test - do it within your Query. You didn't share your query, but it could look something like this:

SELECT test1MeterVol, test1TesterVol, test1Accuracy,
       ((test1MeterVol/test1TesterVol) * test1Accuracy / 100) as test1CorrectedAccuracy
 
FROM [table name]
 
WHERE [some conditions]
Edited by Psycho
Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.