Daniel Brown wrote:
On Dec 3, 2007 10:56 AM, John Taylor-Johnston
<John.Taylor-Johnston@xxxxxxxxxxxxxxxxxxxxx> wrote:
Is there a calculation function?
I'm using an e-commerce shopping cart. I want to tweak the code. The
author is using a varchar(100) field to store prices.
Taking advantage of there being a varchar, instead of entering a price,
I would like to enter a calculation.
(24*2.2)+(24*2.2*.1) 24 is my unit price in British pounds. 2.2 is the
exchange rate into Canadian dollars. etc.
The exchange rate changes frequently. Instead of recalculating and
entering a new price every few days, it would be useful to enter a
calculation in any price field.
I had a look at: http://ca3.php.net/manual-lookup.php?pattern=calc
http://ca3.php.net/manual-lookup.php?pattern=calculate
http://ca3.php.net/manual-lookup.php?pattern=calculation
but I see no function, although I'm sure there is one.
So how could I do this?
$price = (24*2.2)+(24*2.2*.1);
if $price is not an integer, verify if it is a calculation. If so, give
me an integer and round it off to two decimal points:
$price = 58.08;
Do-able?
John
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
John,
Just some example code that may help you out.
<?
$price[] = (24*2.2)+(24*2.2*.1);
$price[] = 58.07777779;
$price[] = "This is a string.";
function intOrCalc($data) { // The function - name it whatever you want.
if(is_numeric($data)) {
return round($data,2);
} elseif(preg_match('/\([0-9].*[0-9]\)/',$data)) {
return $data;
}
}
for($i=0;$i<count($price);$i++) {
echo intOrCalc($price[$i])."\n";
}
?>
I think you may have wanted to have that first line enclosed with quotes.
And have it act/look like you took it from the DB. Correct me if I'm wrong. Should it not be..
$price[] = '(24*2.2)+(24*2.2*.1)';
Other wise, the amount is being evaluated when you assign it to the array.
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php