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

Reply via email to