Jump to content

Simple session -checkout-


menios

Recommended Posts

I ve managed to setup my supersimple shopping cart it works up to the point of storring the items and quantities the user has selected.

Its based on sessions.What i want to do next is when the user hits the procceed button to be prompted to fill a from with his/her details and then store those items in the DB. my knowledge is limited on how to do that.

Can i have some help pls?

Heres is my code in case it helps.

cart.php

 

[hr]<?php
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();
// Process actions
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
	if ($cart) {
		$cart .= ','.$_GET['pd_id'];
	} else {
		$cart = $_GET['pd_id'];
	}
	break;
case 'delete':
	if ($cart) {
		$items = explode(',',$cart);
		$newcart = '';
		foreach ($items as $item) {
			if ($_GET['pd_id'] != $item) {
				if ($newcart != '') {
					$newcart .= ','.$item;
				} else {
					$newcart = $item;
				}
			}
		}
		$cart = $newcart;
	}
	break;
case 'update':
if ($cart) {
	$newcart = '';
	foreach ($_POST as $key=>$value) {
		if (stristr($key,'qty')) {
			$pd_id = str_replace('qty','',$key);
			$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($id != $item) {
					if ($newcart != '') {
						$newcart .= ','.$item;
					} else {
						$newcart = $item;
					}
				}
			}
			for ($i=1;$i<=$value;$i++) {
				if ($newcart != '') {
					$newcart .= ','.$pd_id;
				} else {
					$newcart = $pd_id;
				}
			}
		}
	}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="css/styles.css">
</head>

<body>

<div id="contents">
<h1>Your Shopping Cart</h1>
<?php
echo writeShoppingCart();
echo showCart();
?>
<p><a href="iframe.html" target="display">Continue Shopping</a></p>

</div>

</body>
</html>

 

 

And Functions.php

 

 

[hr]<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
	return '<p>Your Cart is Empty</p>';
} else {
	// Parse the cart session variable
	$items = explode(',',$cart);
	$s = (count($items) > 1) ? 's':'';
	return '<p>Your Cart contains <a href="cart.php">'.count($items).' book'.$s.' </a></p>';
}
}

function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
	$items = explode(',',$cart);
	$contents = array();
	foreach ($items as $item) {
		$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
	}
	$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
	$output[] = '<table  border="0">';
	$output[] = '<tr>';
	$output[] = '<th bgcolor=#62aac4><strong>Book</strong></th>';
	$output[] = '<th bgcolor=#62aac4>Price</th>';
	$output[] = '<th bgcolor=#62aac4>Quantity</th>';
	$output[] = '<th bgcolor=#62aac4 colspan=3> </th>';

	$output[] = '</tr>';
	foreach ($contents as $pd_id=>$qty) {
		$sql = 'SELECT * FROM tbl_product WHERE pd_id = '.$pd_id;
		$result = $db->query($sql);
		$row = $result->fetch();
		extract($row);
		$output[] = '<tr>';
					$output[] = '<td align=center><strong>'.$pd_name.'</strong></td>';
		$output[] = '<td align=center>£'.$pd_price.'</td>';
		$output[] = '<td align=right ><input type="text" name="qty'.$pd_id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
		$output[] = '<td align=right><i>£'.($pd_price * $qty).'</i></td>';
		$total += $pd_price * $qty;
		$output[] = '<td align=center><button type="submit">Add</button></td>';
		$output[] = '</form>';
		$output[] = '<form action="cart.php?action=delete&pd_id='.$pd_id.'" method="post" id="cart">';
		$output[] = '<td align=center><button type="submit" class="r">Remove</button></td>';
		$output[] = '</tr>';
	}
	$output[] = '</table>';
	$output[] = '<p><pre><strong><i>                             Total: <big></i>£'.$total.'</strong></big></pre></p>';

	$output[] = '</form>';
	$output[] = '<pre>                                <button id="checkout" type="submit">Checkout</button></pre>';
} else {
	$output[] = '<p>Your Cart is empty.</p>';
}
return join('',$output);
}
?>

Link to comment
Share on other sites

I ve created the form also and added  <?php $_SESSION['cart'] = $cart;?> so this will import the cart details if i m not mistaken ??? <-- IS this correct?

so now i ll need to do the form validation and then insert everything in the database.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.