简体   繁体   中英

PHP & SQL - select all from a db but loop through results by grouped content

I am creating an ecommerce site where we have suppliers that give us prices in different currencies. We might have one supplier that gives us prices in £-GBP and another that gives us prices in €-EUR and other suppliers where they give us prices in both currencies. If they provide prices in both currencies then our GBP customers get the GBP prices and our EUR customers get the EUR prices. If the price is given to us in just one currency then we do the exchange ourselves.

So my database with all of the pricing looks something similar to this table, with two records for the same product if the supplier gives us prices in both currencies:

 thead{ background-color:#bbb; } td{ padding:10px; }
 <table border='1'> <thead> <tr> <td>Supplier Type</td> <td>Currency</td> <td>Product</td> <td>Attribute</td> <td>Cost</td> </tr> </thead> <tr> <td>Supplier-£</td> <td>£</td> <td>Hat</td> <td>Red</td> <td>10</td> </tr> <tr> <td>Supplier-£</td> <td>£</td> <td>Hat</td> <td>Blue</td> <td>9</td> </tr> <tr> <td>Supplier-€</td> <td>€</td> <td>Shoes</td> <td>Large</td> <td>50</td> </tr> <tr> <td>Supplier-€</td> <td>€</td> <td>Shoes</td> <td>Small</td> <td>45</td> </tr> <tr> <td>Supplier-Both</td> <td>£</td> <td>Suit</td> <td>Large</td> <td>150</td> </tr> <tr> <td>Supplier-Both</td> <td>€</td> <td>Suit</td> <td>Large</td> <td>160</td> </tr> <tr> <td>Supplier-Both</td> <td>£</td> <td>Suit</td> <td>Small</td> <td>100</td> </tr> <tr> <td>Supplier-Both</td> <td>€</td> <td>Suit</td> <td>Small</td> <td>110</td> </tr> </table>

I am looping through this table then to generate a form to update this. However, the way I'm doing it at the moment is creating a line for each line in the DB, I would like to be able to loop through this in a way that is not creating a new line for the same product just because it is another currency. Instead I would like to loop through this so that it creates a line for each product but if that product has costs in both currencies then it should just create two inputs on the one line.

so i would create line for each product and create a dropdown list with the currency values ( can be added dynamic ). its not the easiest way but make it possible to manage the data in consumer way

strcture setup : Example

Old answer :

you should split the table into a product table and a price table. So you can join all prices to the product without handling the association.

Prices
-
PriceID PK int
Price float
Currency money
ProductID int FK >- Product.ProductID

Product
------------
ProductID PK int
Name varchar(200) UNIQUE

EDIT: as mentioned in the comment, the table is splitted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM