Re: [PHP] calculate a varchar
I won't say you CAN'T do it (you can with eval) but you'd be WAY better off to use some kind of exchange rate field that you can change frequently instead of cramming formulas into the field. If eval is the answer, you are probably doing something wrong. You may also want to store everything as cents (or pence) and only insert the decimal on display to avoid rounding errors. On Mon, December 3, 2007 9:56 am, John Taylor-Johnston 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 -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] calculate a varchar
2007. 12. 3, hétfő keltezéssel 15.00-kor Andrew Ballard ezt írta: On Dec 3, 2007 10:56 AM, John Taylor-Johnston [EMAIL PROTECTED] 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 John, Technically, yes you can do it. eval() will work, as Richard mentioned. However, your question involves two things that I prefer to avoid at all costs. The first is storing numeric data in a varchar field. I'm not sure why the author chose this approach. A varchar(100) is reserving storage space for 100 characters. I doubt you're selling any items that require that number of digits, so it's wasted space. (It also makes queries like this SELECT * FROM `items` WHERE `price` 100 problematic because the comparison is being done alphanumerically rather than just numerically.) Price is a number, regardless of the units -- even cats or canaries -- and is nearly always handled better as such. AFAIK the varchar(100) fields do not reserve the space in advance. these fields occupy space according to the length of the data stored in them. hence their name 'var' The second item I like to avoid is anything like that evalutates a string of text as code like eval(), because you have to handle a wide range of potential errors in the string being evaluated as well as the potential security risk. If you're just wanting to convert prices from one currency to another, isn't the formula the same regardless of the currency? Could you not store all your prices numerically in a single currency and then convert the price to any other currency by passing the stored price and the exchange rate into a function? function exchangeCurrency($amount, $exchange_rate) { return $amount * $exchange_rate; } however I agree 100% with you in your conclusions ;) greets Zoltán Németh Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] calculate a varchar
On Dec 3, 2007 10:56 AM, John Taylor-Johnston [EMAIL PROTECTED] 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.0779; $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;$icount($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
Re: [PHP] calculate a varchar
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; You may want to look at eval(). -- Richard Heyes http://www.websupportsolutions.co.uk Knowledge Base and HelpDesk software that can cut the cost of online support ** NOW OFFERING FREE ACCOUNTS TO CHARITIES AND NON-PROFITS ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] calculate a varchar
Daniel Brown wrote: On Dec 3, 2007 10:56 AM, John Taylor-Johnston [EMAIL PROTECTED] 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.0779; $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;$icount($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
Re: [PHP] calculate a varchar
On Dec 3, 2007 10:56 AM, John Taylor-Johnston [EMAIL PROTECTED] 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 John, Technically, yes you can do it. eval() will work, as Richard mentioned. However, your question involves two things that I prefer to avoid at all costs. The first is storing numeric data in a varchar field. I'm not sure why the author chose this approach. A varchar(100) is reserving storage space for 100 characters. I doubt you're selling any items that require that number of digits, so it's wasted space. (It also makes queries like this SELECT * FROM `items` WHERE `price` 100 problematic because the comparison is being done alphanumerically rather than just numerically.) Price is a number, regardless of the units -- even cats or canaries -- and is nearly always handled better as such. The second item I like to avoid is anything like that evalutates a string of text as code like eval(), because you have to handle a wide range of potential errors in the string being evaluated as well as the potential security risk. If you're just wanting to convert prices from one currency to another, isn't the formula the same regardless of the currency? Could you not store all your prices numerically in a single currency and then convert the price to any other currency by passing the stored price and the exchange rate into a function? function exchangeCurrency($amount, $exchange_rate) { return $amount * $exchange_rate; } Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php