Jump to content


Check Out Script


4 replies to this topic

#1 mbx5nitro

    Young Padawan

  • Members
  • Pip
  • 113 posts
  • Gender:Male
  • Location:Houston, TX

Posted 01 May 2006 - 06:28 PM

I have got this script that is a simple PHP cart. It has almost everything except a check out.

I have been trying to make a check out script for it for sometime now although i cant figure i out.

Here is my cart page
<?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['id'];
		} else {
			$cart = $_GET['id'];
		}
		break;
	case 'delete':
		if ($cart) {
			$items = explode(',',$cart);
			$newcart = '';
			foreach ($items as $item) {
				if ($_GET['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')) {
				$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 .= ','.$id;
					} else {
						$newcart = $id;
					}
				}
			}
		}
	}
	$cart = $newcart;
	break;
}
$_SESSION['cart'] = $cart;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Roberts Tropical Fish</title>
	<link rel="stylesheet" href="css/styles.css" />
</head>

<body>

<div id="shoppingcart">

<h1>Your Order</h1>

<?php
echo writeShoppingCart();
?>

</div>

<div id="contents">

<h1>Please check quantities...</h1>

<?php
echo showCart();
?>

<p><a href="index.php">Back to price list...</a></p>

</div>

</body>
</html>
And here is my Functions page.
<?php
function writeShoppingCart() {
	$cart = $_SESSION['cart'];
	if (!$cart) {
		return '<p>You have no items in your shopping cart</p>';
	} else {
		// Parse the cart session variable
		$items = explode(',',$cart);
		$s = (count($items) > 1) ? 's':'';
		return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a><br ><a href="logout.php">Logout</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>';
		foreach ($contents as $id=>$qty) {
			$sql = 'SELECT * FROM cvs_data WHERE id = '.$id;
			$result = $db->query($sql);
			$row = $result->fetch();
			extract($row);
			$output[] = '<tr>';
			$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
			$output[] = '<td>'.$item.'</td>';
			$output[] = '<td>'.$description.'</td>';
			$output[] = '<td>$'.$price.'</td>';
			$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
			$output[] = '<td>$'.($price * $qty).'</td>';
			$total += $price * $qty;
			$output[] = '</tr>';
		}
		$output[] = '</table>';
		$output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>';
		$output[] = '<div><button type="submit">Update cart</button></div>';
										  $output[] = '<div><a href="cart.php?action=addorder">Check Out</a></div>';
		$output[] = '</form>';
	} else {
		$output[] = '<p>You shopping cart is empty.</p>';
	}
	return join('',$output);
}

?>

Those are the only 2 files i see that need to be edited. If someone can help me could you please. I dont care if it goes to cart.php?action=blah or if it goes to checkout.php for the check out script. what ever is easier.

Thanks
zack

#2 Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 03 May 2006 - 02:08 AM

What do you mean by "check out", do you mean just to log out or destroy the session? In case, I think you can use session_destroy();

Edited by Indigo, 03 May 2006 - 02:10 AM.


#3 mbx5nitro

    Young Padawan

  • Members
  • Pip
  • 113 posts
  • Gender:Male
  • Location:Houston, TX

Posted 04 May 2006 - 03:26 PM

its a shopping cart so i want a script to send the info of what the user is ordering. a check out like an online store although no money will be sent just and email to me saying what they ordered and a bill to be sent later.

#4 Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 05 May 2006 - 06:44 AM

If you want a mail about someone ordered, why don't set up your script to send an email? There should be some tutorials on how to send mail in tutorial search, or you can check out the php-tutorials section of these forums, I wrote a tutorial on how to do it a while ago.

Or do I misunderstand (again)?

#5 mbx5nitro

    Young Padawan

  • Members
  • Pip
  • 113 posts
  • Gender:Male
  • Location:Houston, TX

Posted 05 May 2006 - 10:57 PM

I got it all done. I read some of the php manual again lol. All works.

Thanks guys





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users