I have a cart class, where I store the product ids. The user can open the cart through a button that opens the cart in a popup, so the cart content has to be loaded in every page.
I can store only the ids of the products because of the size limits, but I want to show the product names to the final user, so for each element I need to make a query and get the product data from the database.
Is there any major drawback by doing this? Or are there better solutions?
PS: I could get the id of all the products in the cart, and then do one single query that gets the data of any needed product. I’d rather avoid this since I would need to rewrite parts of the class, so is there any actual difference with the previous solution?
PPS: The total number of sql queries shouldn’t be too high in any case. Of course I wouldn’t mind, but I strongly doubt any user will purchase hundreds of different products at one time.
I only like to highlight one sentence from your question:
That is your problem. You do not need to do that. You can query your database with a single query and ask for all products that are in a list of IDs.
A helpful part of the SQL language is the
IN(....)clause.This is a single query for a list of product IDs. When you fetch the data from the data-base you create a in-memory-database on the fly (aka Hashtable) so that you can “fetch” data based on ID:
You also know it as
array, just keyed by the ID value returned from the database. As the ID is unique, this just workstm.Hope this is helpful. Some might it call premature optimization, however, you should be aware of the concept, because this can be used in many cases.