Reputation: 7309
If this question seems like a possible duplicate, I'm sorry. please link me to the original.
I am developing a shopping cart with php and mysql.
In the admin panel, multiple currency types can be set, and new currency types can be added. It will have a default currency and a coversion rate. My problem is when changing the default currency type.
This is what my tables look like
**Currency**
------------------------------------------------------------------------------
ID CurrencyType ConversionRate IsDefault
------------------------------------------------------------------------------
1 type1 - y
2 type2 2.0 n
3 type3 3.0 n
------------------------------------------------------------------------------
**Product**
------------------------------------------------------------------------------
ID Rate
------------------------------------------------------------------------------
1 100
2 200
------------------------------------------------------------------------------
Here for the price for product 1 will be
(100) for currency type1
(100 * 2.0) for currency type2
(100 * 3.0) for currency type3
In the admin panel, the conversion rates will added with respect to the default currency type.
Here while changing the default type, I can update the conversion rates in the currency table. But I am having to update the rate in the product table. Changing the rates for all products in the product table, made me feel that I am wrong.
Is there any other better way to do.? please advice.
Upvotes: 2
Views: 1322
Reputation: 7309
I found a solution with which i need not update the product table.. I am gonna have a HIDDEN currency type which will be the default for the calculations.. The rate in the product table will be always be *1 to this currency type
Can make the necessary calculations in the admin panel so that the admin can still enter the rate of the new product in his default currency type..
Upvotes: 0
Reputation: 1533
Now that I understand your question I am relatively sure that you'll have to update both tables. If you simply wanted to display the table to the user in a different conversion rate but keep the underlying cost the same, then you could always store the value in CurrencyType
type1
but convert it to type2
or type3
in php when displaying it to the user. But if you want the actual values in the Product
table to update when you update the default CurrencyType
, and not just the way that they are displayed to the user, then you'll have to update both tables.
Luckily, SQL makes this very easy since all you're effectively doing is multiplying the current Rate value by the ConversionRate you're changing it to. So if you have a php variable storing the ConversionRate
of the new CurrencyType
before you make it default:
UPDATE Product
SET Rate = Rate * oldConversionRate
should update all rows in the Product table with the new Product
rates. Let me know if that was confusingly worded or you'd like more clarification.
Upvotes: 1