Update 'total' field with multiple price fields

ahoy, i am currently developing a cart for a website at my company.
What i am attempting to do is update the grand total field dynamically as the
product total (determined by quantity & price) are being populated.

Each product in the cart is being generated by a foreach (php) reading from a mysql database.

As the total values are not being prepopulated im not exactly sure what the logic would be to get the desired values.

I will also mention im relatively new to the whole web development area, i started begining of this year and feel i could still learn alot about the ‘logic’ of a computer.

If anything is unclear about what i stated above feel free to request additional information.

Thanks in advance

is your cart coded in javascript or php?

I hope it’s in php because if it’s in javascript you will probably need a Plan B for those that have javascript turned off in their browsers for whatever reason and besides, a server side cart will be much more secure than a client side cart.

My suggestion would be to build a shopping cart class.

The class could include the following

  1. an associative array with the product_id as key and the quantity for that product as the value

  2. an associative array with the product_id as key and the price of the product when it was added to the cart as the value

  3. class method to calculate the total value of the cart from the arrays in 1)

  4. class methods to add/delete/change quantitiies for items in the cart

  5. class method to empty the cart

  6. class methods to retrieve the cart contents for display purposes.

If you post the code you have so far, it will be easier to help you.

Hi Kalon, thanks for the speedy and helpfull reply!

You actually gave me exactly the information i needed - i just really needed the logic, i believe i should look into arrays more in the future:rolleyes:

Your input is much appretiated.