calculations per each record with addon
I am a novice developing an addon expanding on what was posted here:
https://community.transparent-support.com/responses/search-page-calculations
I'm wondering how I would write a code to calculate per each record. For example, I want to know the ratio between the list and sales price to determine a list to sale ratio for each property (Sale price / List Price). From the post linked above, I understand how to do calculations on the entire search result and display the high low median and average of all the ratios but I'm unsure how to display them on my search results page under each listing.
Thanks for taking the time to view my question!
IF... I really got what you mean, you need to use the global variable "$current_ID".
First, you need to include it as a global at your add-on function. Then you need to use it to query the DB (or use the API system) to get the field value you wish.
For example, considering the simple add-on I created for you...
- include it as a global - modify line 159 to:
- global $config, $api, $current_ID;
- $display .= 'Average: $' . ($total / $number_of_listings) . ' (Listing ID:' . $current_ID . ')' . "\r\n";

Thanks Eduardo! This looks like the info I needed. I will give it a try and report back soon! I really appriciate all your help!

With your help I was able to write this simple function:
- global $api, $current_ID;
- $StartField = 'ListPrice';
- $EndField = 'price';
- $StatusField = 'status';
- $ListPriceResult = $api->load_local_api('listing__read', array('listing_id'=>$current_ID, 'fields'=>array($StartField)));
- $EndPriceResult = $api->load_local_api('listing__read', array('listing_id'=>$current_ID, 'fields'=>array($EndField)));
- $StatusResult = $api->load_local_api('listing__read', array('listing_id'=>$current_ID, 'fields'=>array($StatusField)));
- $ListPrice = $ListPriceResult['listing'][$StartField];
- $EndPrice = $EndPriceResult['listing'][$EndField];
- $Status = $StatusResult['listing'][$StatusField];
- if($Status == "Sold"){
- return number_format(($EndPrice / $ListPrice) * 100) . "%";
- }
- }
Hi Richard,
Glad to know you got it. :)
I make a suggestion... changing the logic you will make it run a bit faster and save memory:
- global $api, $current_ID;
- $StatusField = 'status';
- $StatusResult = $api->load_local_api('listing__read', array('listing_id'=>$current_ID, 'fields'=>array($StatusField)));
- $Status = $StatusResult['listing'][$StatusField];
- if($Status == 'Sold') {
- $StartField = 'ListPrice';
- $EndField = 'price';
- $ListPriceResult = $api->load_local_api('listing__read', array('listing_id'=>$current_ID, 'fields'=>array($StartField)));
- $EndPriceResult = $api->load_local_api('listing__read', array('listing_id'=>$current_ID, 'fields'=>array($EndField)));
- $ListPrice = $ListPriceResult['listing'][$StartField];
- $EndPrice = $EndPriceResult['listing'][$EndField];
- return number_format(($EndPrice / $ListPrice) * 100) . '%';
- }
Well done!
And Happy New Year!!! :)

Thanks! I appreciate the suggestion and will use that approach!
Happy New Year to you as well!
Leave Comment

Comments (5)