Richard Wisniewski

shared this question
6 years ago



Relates to



calculations per each record with addon

I am a novice developing an addon expanding on what was posted here:

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!

Add Comment

Comments (5)


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:

  1. global $config, $api, $current_ID;
- and then as a test, modify line 181 to:

  1. $display .= 'Average: $' . ($total / $number_of_listings) . ' (Listing ID:' . $current_ID . ')' . "\r\n";
If it works (I am assuming it will work!), at each Listing at your search result page, you will have each Listing ID being printed - this means you can use that variable to query the DB (or use the API system) and return another value.


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:

  1. global $api, $current_ID;
  2. $StartField = 'ListPrice';
  3. $EndField = 'price';
  4. $StatusField = 'status';
  5. $ListPriceResult = $api->load_local_api('listing__read', array('listing_id'=>$current_ID, 'fields'=>array($StartField)));
  6. $EndPriceResult = $api->load_local_api('listing__read', array('listing_id'=>$current_ID, 'fields'=>array($EndField)));
  7. $StatusResult = $api->load_local_api('listing__read', array('listing_id'=>$current_ID, 'fields'=>array($StatusField)));
  8. $ListPrice = $ListPriceResult['listing'][$StartField];
  9. $EndPrice = $EndPriceResult['listing'][$EndField];
  10. $Status = $StatusResult['listing'][$StatusField];
  11. if($Status == "Sold"){
  12. return number_format(($EndPrice / $ListPrice) * 100) . "%";
  13. }
  14. }
This function will calculate the List/Sale ratio per each record. Thank you again for your help!


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:

  1. global $api, $current_ID;

  2. $StatusField = 'status';
  3. $StatusResult = $api->load_local_api('listing__read', array('listing_id'=>$current_ID, 'fields'=>array($StatusField)));
  4. $Status = $StatusResult['listing'][$StatusField];
  5. if($Status == 'Sold') {
  6. $StartField = 'ListPrice';
  7. $EndField = 'price';
  8. $ListPriceResult = $api->load_local_api('listing__read', array('listing_id'=>$current_ID, 'fields'=>array($StartField)));
  9. $EndPriceResult = $api->load_local_api('listing__read', array('listing_id'=>$current_ID, 'fields'=>array($EndField)));
  10. $ListPrice = $ListPriceResult['listing'][$StartField];
  11. $EndPrice = $EndPriceResult['listing'][$EndField];
  12. return number_format(($EndPrice / $ListPrice) * 100) . '%';
  13. }
This way, at Listings where $Status is NOT "Status", there is no need to query the DB to grab other value.

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


Attach files...

The file must be a jpg, gif, png, bmp, ico, pdf, doc, rtf, txt, zip or rar no more than 20M