Webshop with checkout

Hi there!

I’m trying to create a simple webshop with a checkout and uptil now everything has gone smooth. The problem is that I am quite new to PHP and therefore my coding skills aren’t that great. What I want to achive with my webshop is that the customers cart gets saved with personal information to a mysql database. I know how to get the personal information saved with some inputfields… but how do I get the cart saved? I was thinking on putting it in an input field or textarea and save it on that way to the database.

I have tried with serializing,however it creates duplicates everything the cart is updated… this is how my code looks like… please help this is the final part of the puzzle.

	if(isset($_SESSION["cart_products"])) //check session var
		$total = 0; //set initial total value
		$b = 0; //var for zebra stripe table 
		foreach ($_SESSION["cart_products"] as $cart_itm)
			//set variables to use in content below
			$product_name = $cart_itm["product_name"];
			$product_qty = $cart_itm["product_qty"];
			$product_price = $cart_itm["product_price"];
			$product_code = $cart_itm["product_code"];
			$product_color = $cart_itm["product_color"];
			$subtotal = ($product_price * $product_qty); //calculate Price x Qty
		   	$bg_color = ($b++%2==1) ? 'odd' : 'even'; //class for zebra stripe 
		    echo '<tr class="'.$bg_color.'">';
			echo '<td><input type="text" size="2" maxlength="2" name="product_qty['.$product_code.']" value="'.$product_qty.'" /></td>';
			echo '<td>'.$product_name.'</td>';
			echo '<td>'.$currency.$product_price.'</td>';
			echo '<td>'.$currency.$subtotal.'</td>';
			echo '<td><input type="checkbox" name="remove_code[]" value="'.$product_code.'" /></td>';
            echo '</tr>';
  			$total = ($total + $subtotal); //add subtotal to total var
  			$serialized_cart = serialize($_SESSION["cart_products"]); 
  			echo $serialized_cart;

		$grand_total = $total + $shipping_cost; //grand total including shipping cost
		foreach($taxes as $key => $value){ //list and calculate all taxes in array
				$tax_amount     = round($total * ($value / 100));
				$tax_item[$key] = $tax_amount;
				$grand_total    = $grand_total + $tax_amount;  //add tax val to grand total
		$list_tax       = '';
		foreach($tax_item as $key => $value){ //List all taxes
			$list_tax .= $key. ' : '. $currency. sprintf("%01.2f", $value).'<br />';
		$shipping_cost = ($shipping_cost)?'Frakt : ' .$currency. sprintf("%01.2f", $shipping_cost).'<br />':'';

What you need are multiple associated tables.
For example you may have a table for customers, one for products and another for orders. Then possibly the ‘cart’ would consist of a look up table that pairs product IDs with order IDs.

Thanks I got that suggestion from elsewhere ;D. How do I get the acutal order to be stored then? Is it by using arrays? How would that look like?

I’ve not actually done a shopping cart before, so someone may have a better idea exactly how to structure it, but something like:-

The customer table may have:-
ID - Name - Address - Whatever else -

The product table:-
ID - Name - Price - Description - Etc...

Order table:-
ID - Date - CustomerID - Etc...

Then to record the cart there is a table that matches products to an order. The columns may be:-
ID - OrderID - ProductID
Each order would have multiple rows in the table, one for every item in the cart.

ID    oid   pid
--    --    --
21    15    43
22    15    67
23    15    71
24    16    17
25    16    48

So here order number 15 had three items in the cart (43, 67 & 71), order 16 had two items (17 & 48).

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.