I need some help with doing some calculations using the values from two different records.
3 posts by 2 authors in: Forums > CMS Builder
Last Post: March 20 (RSS)
I need some help with doing some calculations using the values from two different records. The site is for a sea turtle patrol and we have a table of statistics showing how many turtle nests, total turtle eggs, and how many live turtle hatchlings from those eggs. There is a column which displays the value for percentage of live hatchlings. In order to get that value I need to divide live turtle hatchlings by total turtle eggs then multiply * 100. The percentage field is currently calculated and entered manually. I would like to automate this process and have it done using php, as the records are generated/being displayed in the page. (See attached screenshot of the table and data displayed)
Here is the working code which populates the table:
<?php foreach ($annual_statistics_dataRecords as $record): ?>
<tr>
<td class="boldText" align="center"><?php echo htmlencode($record['data_year']) ?></td>
<td align="center"> </td>
<td align="center"><?php echo htmlencode($record['total_number_of_nests']) ?></td>
<td align="center"><?php echo htmlencode($record['total_number_of_eggs']) ?></td>
<td align="center"><?php echo htmlencode($record['total_live_hatchlings']) ?></td>
<td align="center"><?php echo htmlencode($record['percentage_of_live_hatchlings']) ?></td>
</tr>
<?php endforeach ?>
So I need to take ($record['total_live_hatchlings']) and divide that by ($record['total_number_of_eggs']) then multiply by 100 and then display that value instead of the (manually entered) ($record['percentage_of_live_hatchlings'])
Hopefully this all makes sense. Anyone know how to accomplish this
Thanks in advance for any help!
Hello kevbarker,
I believe replacing your calculation table cell with the following should do the trick. I am assuming you just need it for the display and not in the database.
<td align="center"><?php echo htmlencode( (intval($record['total_live_hatchlings']) / intval($record['total_number_of_eggs'])) * 100 ) ?></td>
So here we are taking each record column, converting them to integers, divide the total hatchlings by the number of eggs and multiply by 100. That will give you a floating point value. If you need to round it, there is the PHP round function you can use to do that as well.
Hopefully this is what you were looking to do.
Senior Web Programmer
Interactivetools.com