joejac
Posts: 242
|
| Posted: 06/05/2009, 8:00 PM |
|
Hello everybody
I have a table of materials, like this:
material_id
material_name
material_formula
The material formula is in fact a price calculation, because price varies with material and other parameters form the parameter table.
The following is an example of what can be the material_formula
((((((paramUSD+2)*3,15)*((3/4)*1,015))/10)+param_work)*param_profit)
This formula can vary according to the material_id
I need to execute the formula and to present the price, so:
1.- I can read the parameters from the parameters table,
2.- I can read the material_formula each time the user changes a listbox, but this is a varchar field, so my question is:
3.- In PHP, how can I use the content of this field, material_formula, for the calculation of the complete formula each time the user changes the listbox selection?.
I have access to the MySQL database, so I can change the formula and place PHP variables inside like $paramUSD for instance.
Thanks a lot for any help.
Best regards
joejac
|
 |
 |
jjrjr1
Posts: 942
|
| Posted: 06/05/2009, 10:07 PM |
|
Two ways I can think of quickly.
1. Call an ajax function to do the calculation and have it returned to you page on list box change.
2. Put the formula from the table into a javascript function and call it when the listbox changes. This would be a completly page sepcific DOM action.
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
joejac
Posts: 242
|
| Posted: 06/08/2009, 8:57 AM |
|
Hello John,
Thanks a lot for your time. I liked your suggestion No.1.
1.- I ran the Ajax Feature Builder -Autofill
2.- The Autofill feature returned fine the raw formula, to the calling form, but no evaluation.
3.- I searched in Internet and I found eval() please see: http://uk.php.net/manual/en/function.eval.php
4.- I did a test page like this:
<?php
$paramUSD = 6;
$param_work = 3;
$param_profit = 2;
$MFormula = "(((((($paramUSD+2)*3.15)*((3/4)*1.015))/10)+$param_work)*$param_profit);";
echo $MFormula; echo "<br />";
$MFormulaValue = eval('echo' . $MFormula );
echo "<br />";
?>
4.1 The above code works perfect in the simulation, then
5.- In the Ajax service page I created a custom code in BeforeShow event of the Label that returns the formula to the form with this code:
$paramUSD = 6;
$param_work = 3;
$param_profit = 2;
$MFormula = $Component->GetValue(); // Retrieves the raw formula from the database where it is stored.
$MFormulaValue = eval('echo' . $MFormula );
$Component->SetValue($MFormulaValue);
5.1 I tried in many ways, also including the eval inside the SetValue() of the component and nothing happens.
5.1.1 Probably echo can not be used for a variable or I have a syntax error.
5.1.2 Or the $params variables are not replaced by its values in the formula.
5.2 Unfortunately it works in test php page but not in the Ajax Service page and I do not know
how to make a debug of something that runs exclusively on the server.
5.3 The Ajax service page is returning the raw field good.
I appreciate a lot your kind help, if I finish with this I am done.
Best regards
joejac
|
 |
 |
jjrjr1
Posts: 942
|
| Posted: 06/08/2009, 3:23 PM |
|
Hi
In your AJAX service page try
echo $MFormulaValue;
exit();
instead of:
$Component->SetValue($MFormulaValue);
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
jjrjr1
Posts: 942
|
| Posted: 06/08/2009, 3:28 PM |
|
Oh and maby change this:
$MFormulaValue = eval('echo' . $MFormula );
to this:
$MFormulaValue = eval($MFormula );
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
jjrjr1
Posts: 942
|
| Posted: 06/08/2009, 3:34 PM |
|
BTW
Good job in finding the eval() function in PHP. That is the way to do it in the option #1. The second option would have been javascript execution by virtually the same method.
John
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
joejac
Posts: 242
|
| Posted: 06/08/2009, 5:13 PM |
|
Thanks a lot John, very nice advice.
I did not think about the exit. Now I can Debug.
I found that eval('echo' . $MFormula ); works but it terminates the Ajax Service page because the ; inside the SetValue(), either way works but terminates:
$MFormulaValue = eval('echo' . $MFormula );
$Component->SetValue($MFormulaValue);
or
$Component->SetValue(eval('echo' . $MFormula ));
The error is:
799.721731343.499114 // The correct value is: 799.72173 but with the error
Parse error: syntax error, unexpected $end, expecting ',' or ';' in W:\www\test\project\services\form_material_id_PTAutoFill1_events.php(35) : eval()'d code on line 1
And is the line where is: eval('echo' . $MFormula ) ;
If I take out or escape the ; it do not work.
What can I do John? I am so close to finish...
Best regards
joejac
|
 |
 |
jjrjr1
Posts: 942
|
| Posted: 06/09/2009, 5:37 AM |
|
Try it as I mentioned before take the 'echo' out of the eval and echo just the value instead of setting a component
$MFormulaValue = eval($MFormula );
echo $MFormulaValue;
exit();
in your service page instead of
$MFormulaValue = eval('echo' . $MFormula );
$Component->SetValue($MFormulaValue);
Might work
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
jjrjr1
Posts: 942
|
| Posted: 06/09/2009, 5:42 AM |
|
If for some reason you need that 'echo' in your eval....
Try: $MFormulaValue = eval('echo ' . $MFormula . ";");
Adding a space after echo and adding a quoted semi-colon after $MFormula.
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
joejac
Posts: 242
|
| Posted: 06/10/2009, 11:11 AM |
|
Thanks a lot John for your kind help and time.
In my last post I forgot to tell you that I tried that too with no luck.
1.- Finally after many hours lost, what worked was this:
$MFormula = $Component->GetValue();
eval("\$MFormulaValue = $MFormula;" );
$Component->SetValue($MFormulaValue);
2.- After this solution and a terrible headache I Googled more and I found this:
"I should note that by default the result of eval() is sent directly to the browser . If you want to capture the result, do something like the following:"
<?php
//capture results of eval()
$num = 3;
ob_start();
eval("include 'http://www.example.com/myScript.php?num=$num';");
$result = ob_get_contents();
ob_end_clean();
?>
Source: http://www.nicoleswan.com/archives/2004/07/advanced-php-eval/
3.- This was not documented in php.net function. This is not the eval function, it is the evil function....
Regards
joejac
|
 |
 |