0

Hi all,
I am trying two days now to insert a cart link on my page. It is a e-shop bookstore.
I save my books in a database :
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| bookid | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(200) | NO | | NULL | |
| author | varchar(100) | NO | | NULL | |
| publisher | varchar(100) | NO | | NULL | |
| catid | int(11) | NO | | NULL | |
| cost | float | NO | | NULL | |
| isbn | varchar(10) | NO | | NULL | |
| description | text | NO | | NULL | |
| weight | int(5) | YES | | NULL | |
| nopages | int(4) | YES | | NULL | |
| imagename | varchar(100) | NO | | NULL | |
| sales | int(5) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+

And I have a detail page for book with a link "order".
When a user clicks on order then cart.php page opens.

The link order is ::

echo '<a href="cart.php?action=add&id='.$isbn.'" class="more"><img src="images/order_now.gif" alt="" title="" border="0" /></a>';

While the cart.php is ::

<?php

include_once('header.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 books WHERE isbn = ".$id."";
			$result = 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>'.$title.' by '.$author.'</td>';
			$output[] = '<td>&pound;'.$price.'</td>';
			$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
			$output[] = '<td>&pound;'.($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);
}


// 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="index.php">Back to bookshop...</a></p>

</div>

</body>
</html>

When I am tryng to add a new item a take
Your Shopping Cart

You have 16 items in your shopping cart
Please check quantities...

Fatal error: Call to undefined function query() in C:\wamp\www\book_store\cart.php on line 33


Could you help me please do it ?
Is there simpler way to do this ??

Thanks a lot

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by bufospro
0

Hi,

If it's a mysql database, then change "query($sql);" to "mysql_query($sql);"

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.