E Steven
E Steven

Reputation: 81

Help Storing Cart Items In Session/Database

I am using Kohana, but i think this question could pertain to PHP in general.

I am having trouble conceptualizing how to do this - I need to store items in a cart(I know, basic stuff here), and can't seem to think of the best way to store/retrieve this information. I can store a count of cart items just fine, but can't see how to store each individual item in the cart (one idea i had was to store each with the key of item_1, item_2 etc). Other idea was to store in an array, but i have no idea how to store an array in the database, and how to then retrieve it. Noob question, I know, but any help would be greatly appreciated.

Upvotes: 2

Views: 1140

Answers (1)

user432219
user432219

Reputation:

To store a cart and its items into a database create 3 tables:

Cart        CartItem      Product
========    ==========    =========
CartID   <- CartID        ProductName
            ProductID  -> ProductID
            CartItemID

The much better way is to store the array in a session. You can use PHP built-in functions for that:

<?php

session_start();

include "./get_cart_items.php";

// write to session
$_SESSION['cart'] = $cartItemsArray;

To read the content later:

<?php

session_start();

// read from session    
$cartItemsArray = $_SESSION['cart'];

Upvotes: 1

Related Questions