Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8626681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:05:36+00:00 2026-06-12T08:05:36+00:00

I have a cart class, where I store the product ids. The user can

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:05:38+00:00Added an answer on June 12, 2026 at 8:05 am

    I only like to highlight one sentence from your question:

    so for each element I need to make a query and get the product data from the database.

    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.

    $ids = [123, 884, 7848, 2882, 3232]; // let's say that is your input
    $idList = implode(',', array_map('intval', $ids));
    $sql = sprintf(
        "SELECT field1, field2, field3 FROM products WHERE products.ID IN(%s)",
        $idList
    );
    

    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:

    foreach($ids as $id)
    {
        $concreteProduct = $rows[$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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cart table which has a relation to a product table that
I have a JTable called Cart where user can add products before buying them.
I have a url that looks like this: http://localhost/store/mens/category/t-shirts/item/a-t-shirt I have a class called
I have a cart model class that has a List property like so: public
I have made a cart and the cart has remove item button, but the
I have a online shopping cart, at checkout user enters his zipcode. There are
I have function getCartItems in cart.js and I want to call that function in
I have an asp based shopping cart that uses html based templates to display
I'm building an ASP.Net website. I have a cart class which stores the items
We have a very simple method that uses findById. public Cart getCart(long cartId) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.