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"; } ?> -- Daniel P. Brown [office] (570-) 587-7080 Ext. 272 [mobile] (570-) 766-8107 If at first you don't succeed, stick to what you know best so that you can make enough money to pay someone else to do it for you. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php