FIRST: Would it be better to use sessions to store shopping cart information, or would it be better to store them into the database when a product is added to the cart? Or is one not better than the other?

Can someone point me in the right direction, on how I add more data to the session set, or pass a new session to the cart I am trying to build.

Currently, I am letting the user click a form submit button and pass the data via $_POST.

I then get the produkt_id from the $_POST - And get the relevant data to display in the shopping cart. - So far so good..

Then I also need to pass on the size, which is from a different table, and I get the size from a select list which is also in the form.

So far I can add products, get the information - because the information is all in the same table. But how do I save the sizes in a session too, and add that to the cart (Sizes are stored in another table, and have produkt_id in commen).

For getting the information without the sizes i have this:

// Add to cart:
if (isset($_POST['kom_i_kurv'])) // START SHOPPING CART:
	$str = mysqli_real_escape_string($connection,$_POST['sizes']);
	$produkt_id_til_kurv = mysqli_real_escape_string($connection, $_POST['produkt_id']);
	// Valider:
	if ($str == 'nej')
		$error_class = 'class="kom_i_kurv_fejl"';
                  if (empty($error_class))

// Tjeck how many products are in stock, and dont allow to add more than that:
$quantity = mysqli_query($connection, "
SELECT produkt_id, stock FROM products WHERE produkt_id = '".(int)$produkt_id_til_kurv."'");
while ($quantity_row = mysqli_fetch_assoc($quantity))
if ($quantity_row['stock'] != $_SESSION['cart_'.$produkt_id_til_kurv]) 
	$_SESSION['cart_'.(int)$produkt_id_til_kurv] += '1'; 
	else echo '<p>Der er desværre ikke flere på lager i øjeblikket!</p>';

This lets me add the products as long as there are any on stock.

But how do I also pass in the sizes kept in the variabel: $str ????

In the cart, i display the informartion like this, and here I also need to pass in the sizes for each product added to the cart.

// Display Cart:
function cart($connection)
foreach ($_SESSION as $name => $value)
{   // $name = name of session, and $value holds what kept in the session:
if ($value>0)
{  // Session is set:
if (substr($name,0,5) == 'cart_') 
// Use only values from cart_
// WOULDNT IT BE HERE I SHOULD SOMEHOW INCLUDE THE SIZES TOO?? ///////////////////////////////////
$id = substr($name, 5, (strlen($name)-5)); // Anything after cart_ (1,2,or 43267 - product_ids, red)
// Get information for each product from product table
$sql = mysqli_query($connection, "SELECT * FROM products WHERE produkt_id = '".$id."'");
while ($get_row = mysqli_fetch_assoc($sql))
//echo '<img src="admin/product_img/thumbs/'.$get_row['produkt_id'].'.'.$get_row['img'].'" />'.'<br />';
// Get brand name from brands table:
$sql2 = mysqli_query($connection, "SELECT navn FROM brands WHERE brand_id = '".$get_row['brand_id']."'");
while ($get_row2 = mysqli_fetch_assoc($sql2))
$brand_navn = $get_row2['navn'];
// Get category from category table:
$sql3 = mysqli_query($connection, "SELECT navn FROM vare_grupper WHERE id = '".$get_row['type_id']."'");
while($get_row3 = mysqli_fetch_assoc($sql3))
$vare_type = $get_row3['navn'];
echo ucfirst($brand_navn) .' - ' .$vare_type.'<br />';
echo 'Model: ' .$get_row['link_navn'].'<br />';
// HERE I WANT TO ECHO OUT THE SIZE ////////// (Medium, Largetc) //////////////////////// //
echo 'Antal: '.$value.' '.'<br />'; //////////// THIS IS THE QUANTITY - SO 2 * MEDIUM if two is chosen..
echo 'Enheds pris: Kr. '.number_format($get_row['pris'],2).',-'.'<br />';
$sub_total = $get_row['pris'] * $value; 
echo 'Subtotal: Kr. '.number_format($sub_total,2).',-'.'';
echo '<br /><br /><hr /><br />';
		} else {
			echo '<p>Der er ingen varer i indkøbskurven!</p>'; // Nothing in basket text, red.

I hope you get the point..

Basically, how do I also add the sizes in the script at the top, pass it in a session - and how do i display them with the product info from the script below?

Remember the sizes are from a different table, and can be:
Or for jeans:
30/30 - 30/32 and loads more - so I made the decicion to keep them in a seperat table with a product id, isnt that the best way to do it or?


Well, it is better to store the information in a session variable. But you create a variable such as $_SESSION and store an awway of products in the cart in that. for example.

$_SESSION['cart'] = array(1,5,9,5);

Where the array contains all the product id's that are in the cart.

If you have product attributes you could do something like this:

$_SESSION['cart'] = array(
              [1] = array(2,2),
              [2] = array(4,0)

There is probably a better method, but this should work. Basically the first element in the sub array is the product id, and the second element would be the id of the attribute if there is any. If not that would either be null or 0, and just make a if statement to test fot this.

Example usage would be:

foreach ($_SESSION['cart'] as $item) {
     $productid = $item[1];
     $productattr = $item[2];

Please dont hesitate to ask any more questions. :)


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.