PHP Form script to perform calculation.

mamies

Member
Hi All,

I have decided to make the move and use the latest Fabrik for an upcoming project I am working on. The idea of how this is supposed to work is that it will have rainfall and evaporation data entered and it will calculate the effective amount of moisture that is in the ground.

The formula is fairly easy and is basically the amount of rainfall that has been obtained - 5mm.

I have created the below script which would be something I would use in Joomla 1.5 but this doesn't exactly work for the new version.

Just wondering if someone can point me in the right direction.

PHP:
$db = JFactory::getDBO();
$rainfall = $this->_data['data_global_irrigation___rainfall'];
if (trim($rainfall) < "5") {
$formModel->updateFormData('data_global_irrigation___effective_rainfall', 0);
$formModel->updateFormData('data_global_irrigation___effective_rainfall_raw', 0);
}
else {
$db->setQuery("UPDATE data_global_irrigation SET effective_rainfall = effective_rainfall - 5 WHERE id = $rowid");
$db->query();
}
 
Thanks JFquestiaux.

I didn't realise they had that element plugin. It works perfectly and I like the AJAX update that it includes.

Just wondering if this is able to be used to move information to different tables.

I would like to have a running total of the irrigation. The idea is that every day the rainfall data is added and it adds to the total. Also the evaporation data is also added which will minus from the total.

Thanks,
 
Sorry. I don't think I am very good at explaining this.

The idea of this is that I would like to keep a record of how much water is in the soil in rainfall mm (millimetres). Currently that is done by adding the amount of mm that has fallen over the given day. Since evaporation takes water away I need to be able to subtract that value.

Let me know if that makes any more sense.

Thanks again for your help.
 
I have done some playing and I have come up with this. However I am having issue with the adding of $calc to sm_balance. If I substitute $calc with a number it works fine.

PHP:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$effrain = $formModel->formData['data_global_irrigation___effective_rainfall'];
$evaporation = $formModel->formData['data_global_irrigation___evaporation'];
$calc = $effrain - $evaporation;
$db->setQuery("UPDATE data_pivots SET sm_balance = sm_balance + $calc");
$db->execute();
 
Have you verified $calc contains anything? Try
PHP:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$effrain = $formModel->formData['data_global_irrigation___effective_rainfall'];
$evaporation = $formModel->formData['data_global_irrigation___evaporation'];
$calc = $effrain - $evaporation;
return $calc;
// $db->setQuery("UPDATE data_pivots SET sm_balance = sm_balance + $calc");
// $db->execute();
 
Thanks for your reply rackem.

When using the below it returns the correct value.

When using what you have above the form processes as it was before.

I was thinking that I have the syntax wrong for the $db->setQuery line. More specifically around the $calc variable.

PHP:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$effrain = $formModel->formData['data_global_irrigation___effective_rainfall'];
$evaporation = $formModel->formData['data_global_irrigation___evaporation'];
$calc = $effrain - $evaporation;
echo "<pre>";print_r($calc);exit;
$db->setQuery("UPDATE data_pivots SET sm_balance = sm_balance + $calc");
$db->execute();
 
After reading your previous posts more, I am wondering if the calc element is what you want to use to update your table. Depending on how you have it set up, it runs when you save the form or once for every row displayed in your list. So using it to update your database might not be a good idea. A better option may be the PHP Form Plugin (http://fabrikar.com/forums/index.php?wiki/php-form-plugin/). I don't really understand what you are trying to achieve though from a Fabrik flow standpoint. You may want to use the calc element to perform the calculation and then a PHP Form Plugin to update your other table.

Another thing I noticed was your syntax for effrain and evaporation variable. You can use placeholders with the calc element if you want (http://fabrikar.com/forums/index.php?wiki/placeholders/). I'm not very familiar with the syntax you are using but I think it is an accepted alternate approach and what is used in the PHP Form Plugin.

I don't see anything wrong with your code. The following syntax works for me to execute queries but I think $db->execute(); is the preferred way.

Code:
$query = ("TRUNCATE TABLE `mps_fargo_optimized_rtg`");
$db->setQuery($query);
$db->query();

Last of all, I was curious about your line "UPDATE data_pivots SET sm_balance = sm_balance + $calc". That would update sm_balance in every row of your data_pivots table. Is that intentional?
 
Last of all, I was curious about your line "UPDATE data_pivots SET sm_balance = sm_balance + $calc". That would update sm_balance in every row of your data_pivots table. Is that intentional?

Definitely is intentional. This form is for people to enter the rainfall data for the region which should then automatically be applied to all of the paddocks

I thought about the calc plugin but it will have other inputs from single irrigation fields.

Thanks for looking rackem.
 
Hi Alll... Please help me
I have a database (price_db) which contains colums : user_id(raw), and price. etc...
how to sum the colum of price according to the user_id?
and how to display it in my web?
please help me...
 
Hi guys.

I finally found my answer to why my script wouldn't work.

I needed to have it set to integer to perform any calculations :oops:

Thanks for your help.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top