i have a small problem. i'm writing an online store using PHP/mySQL. to implement the basket, i decided to store parallel arrays in a cookie. one array holds item_id's, the other hold corresponding quantities. in my opinion, most recently added items should appear last on the list in the basket...so when adding an item to the basket, item_id's and quantities are pushed to the end of each array.
here comes the problem...when i want to query the database to get the name and price for each product i use the following SQL statement (where 888, 111, 444, is a list of item_id's in the basket):
SELECT `name`, `price` FROM `items` WHERE item_id IN (888, 111, 444)
i want to query the database only once. i think it would be foolish to run through the item array and query the database for each entry. but when i use that SQL statement, it automatically sorts the results by item_id...so i can't keep the information straight.
at first, i thought i could get the name and price when each item is added, and keep that information in the cookie as well...but what if the information is updated and the cookie info is old? so then i thought, i could query it once (just like above) and store the information in a multidimensional array with item_id's as keys (i don't know the syntax for this yet...but i know it's possible). i would still have to run a loop for each item in the basket...but it seems like the best solution because it only runs to the database once. what do you guys think? any suggestions or ideas are greatly appreciated. thanks in advance.
PHP - implementing a shopping cart/basket
Moderator: General Moderators