My questions are:
1. How would I code a "reset cart" button, to completely destroy the session and start a new one? (Customers do not have accounts on this site)
2. Have I implemented this code incorrectly? Could it be done better? (All variables are sent from the "Add to Cart" buttons on the catalog page)
3. Could I avoid this hassle entirely by using some 3rd party application? (Budget is a major issue)
Using Dreamweaver CS4 on Vista. Any comments or help is much appreciated!
It looks like you are using the session to store HTML output.
I would instead only store an array of product IDs and quantities, e.g.:
PHP Code:
$_SESSION['cart'][1234] = 2; // product ID '1234' with a quantity of '2'
$_SESSION['cart'][2345] = 3; // product ID 2345' with a quantity of '3'
Then for each usage of cart data in your processing and/or page display, you would use the session cart data as the basis for a DB query, e.g. (untested, for ideas only):
This is assuming "serial" is an integer. If a string, some additional processing would be needed for SQL escaping and quoting in the IN() expression.
As far as clearing the cart, all you would need is to do an unset($_SESSION['cart']); to remove all items.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
The serials will be strings, unfortunately. It's the only way my client could track his inventory (taking an established business online)
I've already got the SQL database for the catalog done. There are 5 tables, each for a seperate category of items. Would this possibly make it any simpler?
The serials will be strings, unfortunately. It's the only way my client could track his inventory (taking an established business online)
That is no problem other than a bit more complexity in setting up the queries, as string values need to be filtered and quoted. This could be avoided though if your database structure used integer primary key field in addition to the serial number field. Then you would use that value (the primary key integer) instead of the serial number in the cart session data.
I've already got the SQL database for the catalog done. There are 5 tables, each for a seperate category of items. Would this possibly make it any simpler?
This would make things somewhat hairier, as you would have to somehow also track the category in the session data, and probably run separate queries. A more "normalized" approach would be to have a single product table that stores the data elements common to all products, then possibly have additional tables and possibly relation tables for tracking info specific to different categories and related to the main product table by the product primary key.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
If I streamlined my database structure, listing all products within one table (using other tables for category references), I'd need to:
- be able to list products by category in the catalog page
- use the Add to Cart button to send the ProductID to the cart form, which would then add a row of information based on the information present in the table.
Or, would it be better to have a seperate script process the "Add to Cart" and then send them to the cart page with the updated information?
I've redone my catalog and my database. I now have two tables: "products" which lists all products, with an integer for category_id; and "categories" which assigns integers to all categories. So now all my products have a unique product_id (as an integer)
I've redone my catalog and my database. I now have two tables: "products" which lists all products, with an integer for category_id; and "categories" which assigns integers to all categories. So now all my products have a unique product_id (as an integer)
I think you'll find this sort of normalization will help keep things more manageable. You should now be able to just store the product ID and quantity of each cart item in your session "cart", then use those IDs as needed to query the database for whatever additional info you need to display the cart contents, calculate price sub-totals and totals, etc. in any given script.
If you still need help with specific queries, we may need the details on the current DB structure in order to help.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks