Saving items in a shopping cart

i already made a working shopping cart, but it only calculates and cou

Page 1 of 1

8 Replies - 21536 Views - Last Post: 19 October 2009 - 02:36 AM Rate Topic: -----

#1 fallen0ne09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 13-March 09

Saving items in a shopping cart

Posted 14 August 2009 - 06:03 AM

could anyone recommend me some tutorials about making an online shopping cart for my web... i dont have the first idea what to do... is the shopping cart a database? and i dont know the first thing about creating my own shopping cart.... i seen some free source codes... but i cannot understand it.... could someone please recommend me some tutorials... i really need it... thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Saving items in a shopping cart

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Saving items in a shopping cart

Posted 14 August 2009 - 06:49 AM

SHow us some code you've tried and what you don't understand. We can't do much more for you than search Google, and you can do that yourself.
Was This Post Helpful? 0
  • +
  • -

#3 fallen0ne09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 13-March 09

Re: Saving items in a shopping cart

Posted 13 October 2009 - 03:09 AM

thanks to all who are helping me... and thanks for the watchmaker project for giving me this code....

i have this shopping cart i wish to add up to my website... i works but i want it to have the associate picture and the data... so far only the text can be shown not the picture... here is my code

index.php
<?php


$sql = "select * from item where type = 'Men Shirts' ORDER BY `item`.`id` ";
$result = $db->query($sql);
$output[] = '<ul>';
while ($row = $result->fetch()) {
	
	$output[] = '<li>"'.$row['name'].'" <br> '.$row['type'].'<br>: &yen;'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
}
$output[] = '</ul>';
echo join('',$output);
?> 


this works but when i want to pop also the picture i add the code like this
<?php


$sql = "select * from item where type = 'Men Shirts' ORDER BY `item`.`id` ";
$result = $db->query($sql);
$output[] = '<ul>';
while ($row = $result->fetch()) {
	
	$output[] = '<img src=\"images/"  . $row['filename'] . "\" alt=\"\" width=\"300\" height=\"400\" /><br />'.'<li>"'.$row['name'].'" <br> '.$row['type'].'<br>: &yen;'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
}
$output[] = '</ul>';
echo join('',$output);
?>
and this error message comes out....



Parse error: syntax error, unexpected T_STRING in C:\wamp\www\order1.php on line 119
i dont know whats wrong, and in my cart.php
<?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>PHP Shopping Cart Demo  Cart</title>
	<link rel="stylesheet" href="css/styles.css" />
</head>

<body>

<div id="shoppingcart">

<h1>Your Shopping Cart</h1>

<?php
echo writeShoppingCart();
?>

</div>

<div id="contents">

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

<?php
echo showCart();
?>

<p><a href="order1.php">Back to bookshop...</a></p>

</div>

</body>
</html> 


it already works but i cant seem to save the items inside the cart to the database it only shows it and computes... please tell me what is wrong... i dont know what to do... thanks...

This post has been edited by fallen0ne09: 13 October 2009 - 03:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 fallen0ne09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 13-March 09

Re: Saving items in a shopping cart

Posted 13 October 2009 - 08:39 PM

thanks to the watchmaker project for teaching me this code... i have already a shopping cart that works but it cannot save the items inside the database... could anyone give me some suggestions... here are my code...

index.php
 <?php


$sql = "select * from item where type = 'Men Shirts' ORDER BY `item`.`id` ";
$result = $db->query($sql);
$output[] = '<ul>';
while ($row = $result->fetch()) {
	echo "<div class=\"picture\">";
										echo "<p>";
										 
	$output[] = '<li>"'.$row['name'].'" <br> '.$row['type'].'<br>: '.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
}
$output[] = '</ul>';
echo join('',$output);
?> 
here is my cart.php
<?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>PHP Shopping Cart Demo  Cart</title>
	<link rel="stylesheet" href="css/styles.css" />
</head>

<body>

<div id="shoppingcart">

<h1>Your Shopping Cart</h1>

<?php
echo writeShoppingCart();
?>

</div>

<div id="contents">

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

<?php
echo showCart();
?>

<p><a href="order1.php">Back to bookshop...</a></p>

</div>

</body>
</html>
and here is the functions.php where the cart works...
<?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></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 item 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>'.$name.' <br> '.$type.'</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><button type="submit">Send Order(s)</button></div>';
		$output[] = '</form>';
	} else {
		$output[] = '<p>You shopping cart is empty.</p>';
	}
	return join('',$output);
	
	$sql = "INSERT INTO data (Msg,Date)"
	 . " VALUES ('$msg','$date')";
  if (mysql_query($sql, $con)) {
	print("Saved");
	
  } else {
	print("Sending statement failed.\n");
	print(mysql_errno($con).": ".mysql_error($con)."\n"); 
  }
  
  mysql_close($con); 

}
?>
please see my code... i dont know what to do with it... please help me...

This post has been edited by fallen0ne09: 13 October 2009 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Saving items in a shopping cart

Posted 13 October 2009 - 09:01 PM

Try to add die() at the end of your queries, and also add apostrophe to your Date in your query
....
 $sql = "INSERT INTO data (`Msg`,`Date`)"
     . " VALUES ('$msg','$date')";
  if (mysql_query($sql, $con)) {
    print("Saved");
    ....

and also I didn't found $msg and $date where are they coming from, you didn't declare it befire
:)
Was This Post Helpful? 0
  • +
  • -

#6 fallen0ne09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 13-March 09

Re: Saving items in a shopping cart

Posted 13 October 2009 - 10:17 PM

View Postnoorahmad, on 13 Oct, 2009 - 08:01 PM, said:

Try to add die() at the end of your queries, and also add apostrophe to your Date in your query
....
 $sql = "INSERT INTO data (`Msg`,`Date`)"
     . " VALUES ('$msg','$date')";
  if (mysql_query($sql, $con)) {
    print("Saved");
    ....

and also I didn't found $msg and $date where are they coming from, you didn't declare it befire
:)

sori i got mixed up... that insert into data.. is not associated with the code... i forgot to cut it offf.... i meant was insert into books('name'); . "values ($name);... thats the real thing... becoz i want my shopping cart to save the items in it when click saved... but all it do is just calculate and count the items inside...
Was This Post Helpful? 0
  • +
  • -

#7 fallen0ne09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 13-March 09

Re: Saving items in a shopping cart

Posted 13 October 2009 - 10:22 PM

thanks to all who are helping me and suggesting... but i still cant figure out this problem... i am using a shopping cart for my web.. it works.. .but i cant seem to select the picture of that item only the text... i have this code but it always says an error every time i add the image tag... please help me... here is my code
<?php


$sql = "select * from item where type = 'Men Shirts' ORDER BY `item`.`id` ";
$result = $db->query($sql);
$output[] = '<ul>';
while ($row = $result->fetch()) {
	echo "<div class=\"picture\">";
										echo "<p>";
										 
	$output[] = '<li>"'.$row['name'].'" <br> '.$row['type'].'<br>: &yen;'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>'.'<img src= 'images'  . $row['filename'] . ' alt= '' width= '300'  height= '400' /><br />';
}
$output[] = '</ul>';
echo join('',$output);
?> 
i already added the <img tag... the pictures are already saved in my database ... and it works if i put it in echo... but the pictures and data are separated in output... and here is the error
Parse error: syntax error, unexpected T_STRING in C:\wamp\www\order1.php on line 119
please help me... i cant seem to figure this out...
Was This Post Helpful? 0
  • +
  • -

#8 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Saving items in a shopping cart

Posted 14 October 2009 - 12:42 AM

your problem is in your single and double quotes they when you want to add single quotes inside any string that is rounded by single quotes add \ for escaping from single quote

...'<img src= \'images\''. $row['filename'] .'alt=\'\' width=300 height=400/><br />';...

:)
Was This Post Helpful? 0
  • +
  • -

#9 fallen0ne09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 13-March 09

Re: Saving items in a shopping cart

Post icon  Posted 19 October 2009 - 02:36 AM

i have created a shopping cart, thanks to the watchmaker project... and it works but the problem is it only calculates the price and count the items inside the cart, while the important part is, it cannot save the items inside the cart to the database, could anyone help me here is my code... the order.php
<?php


$sql = "select * from item where type = 'Men Shirts' ORDER BY `item`.`id` ";
$result = $db->query($sql);
$output[] = '<ul>';
while ($row = $result->fetch()) {
	echo "<div class=\"picture\">";
										echo "<p>";
										 
	$output[] = '<li>"'.$row['name'].'" <br> '.$row['type'].'<br>: &yen;'.$row['price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
}
$output[] = '</ul>';
echo join('',$output);
?> 
and here is the function.inc
<?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></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 item 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>'.$name.' <br> '.$type.'</td>';
			$output[] = '<td>&yen;'.$price.'</td>';
			$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
			$output[] = '<td>&yen;'.($price * $qty).'</td>';
			$total += $price * $qty;
			$output[] = '</tr>';
		}
		$output[] = '</table>';
		$output[] = '<p>Grand total: <strong>&pound;'.$total.'</strong></p>';
		$output[] = '<div><button type="submit">Update cart</button></div>';
		$output[] = '</form>';
	} else {
		$output[] = '<p>You shopping cart is empty.</p>';
	}
	return join('',$output);
	
}
function saveCart() {
	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>';
		$name = $name;
		foreach ($contents as $id=>$qty) {
			$sql = "INSERT INTO orders (name)"
	 . " VALUES ($name)";
  if (mysql_query($sql, $con)) {
	print("Saved");
	$output[] = '<div><button type="submit">Send Orders</button></div>';
	$output[] = '</form>';
  } else {
	print("Sending statement failed.\n");
	print(mysql_errno($con).": ".mysql_error($con)."\n"); 
  }
  
  mysql_close($con); 
}

?>
it works but when i added the insert tag... it had given this error
Parse error: syntax error, unexpected $end in C:\wamp\www\inc\functions.inc.php on line 78

and lastly the cart.php where the items are shown cart.php
<?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>PHP Shopping Cart Demo · Cart</title>
	<link rel="stylesheet" href="css/styles.css" />
</head>

<body>

<div id="shoppingcart">

<h1>Your Shopping Cart</h1>

<?php
echo writeShoppingCart();
?>

</div>

<div id="contents">

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

<?php
echo showCart();
?>

<p><a href="order1.php">Back to bookshop...</a></p>

</div>

</body>
</html>
please help me with this code... i dont know whats wrong... thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1