Jump to content

Shopping cart tutorial problems


mr_badger

Recommended Posts

I bought a book called Creating Interactive websites with PHP and web services to which the author is the lead developer on this website.

I have completed the shopping cart tutorial but when I come to test it I get the message Class 'ShoppingCart' not found in/mydir.

I downloaded the script and I have written the code just as the downloaded script.

 

Anyone have this book and solved the problem?

Link to comment
https://forums.phpfreaks.com/topic/54986-shopping-cart-tutorial-problems/
Share on other sites

Sorry I didn't post any code. This is the cart.php

<?php

include $_SERVER['DOCUMENT_ROOT'].
'/layout.php';

$cart = &new ShoppingCart;
$cart_id = $cart->get_cart_id();

switch($_REQUEST['req']){
case "add":
$add2cart = $cart->cart_add($_REQUEST['product_id'], $_REQUEST['qty']);

myheader("Shopping Cart");
if(!$add2cart) {
	echo "<center>The product could not be " . "to your shopping cart. You may " . "have entered an invalid quantity</center>";
}   else {
	echo "<center>Item added to your shopping cart<br />" . "<a href=\"/cart.php\">View ". "your cart</a></center>";
}
footer();
break;

case "update":
while(list($product_id, $qty) = each($_POST[qty])){
	$sql = mysql_query("SELECT * FROM shopping_products WHERE product_id='$product_id'");

	$row = mysql_fetch_assoc($sql);
	if($qty == 0) {
		mysql_query("DELETE FROM shopping_carts WHERE cart_identifier='$cart_id' AND product_id='$product_id'");
}

	if($qty > $row[product_qty]){
		mysql_query("UPDATE shopping_carts SET product_qty='{$row[product_qty]}' WHERE cart_identifier='$cart_id' AND product_id='$product_id'");

		$error = TRUE;
		$products[$product_id] = stripslashes($row[product_title]);
}       else {
		mysql_query("UPDATE shopping_carts SET product_qty='$qty' WHERE cart_identifier='$cart_id' AND product_id='$product_id'");
}
}
if($error){
	myheader("Shopping Cart");
	echo "<center>You have selected more ". "than our current stock for the following ". "product(s): <br />";

	while(list($product_id, $product_name) = each($products)){
		echo "<a href=\"/products.php?require=view&product_id=$product_id\">" . "product_name</a><br />";
}

	echo "<br />";
	echo "We have updated your quantity to the maximum " . "value that we have in stock.</center><br />";
	echo "<center><a href=\"/cart.php\">" . "Back to Cart</a></center>";
	footer();
} else {
	header("Location: /cart.php");
}

break;

case "remove":
   $sql = mysql_query("DELETE FROM shopping_carts WHERE cart_identifier='$cart_id' AND product_id='{$_REQUEST['product_id']}'");
   header("Location: /cart.php");
   
   break;
   
   case "empty_confirm":
   myheader("Shopping Cart");
   echo "<center> Are you sure ". "you want to empty your cart?<br />" . "<a href=\"/cart.php?req=empty\">Yes</a>" . " | " . "<a href=\"/cart.php\"
        >No</a></center>";
        
        footer();
        
        break;
        
        case "empty":
        myheader("Shopping Cart");
        $cart->empty_cart();
        echo "<center>Your cart has been emptied</center>";
        footer();
        
        break;
        
        
        
        default:
        myheader("Your Shopping Cart");
        
        if($cart_id) {
			$num_items = mysql_result(mysql_query("SELECT count(*) as items FROM shopping_carts WHERE cart_identifier='$cart_id'"),0);

			if($num_items == 0) {
				echo "<center>Your shopping cart is empty</center>";
				footer();
				exit();
}
}               else {
			echo "<center>Your shopping cart is empty</center>";
			footer();
			exit;
}




?>

<p><font size="4" face="Verdana, Arial, Helvetica, sans-serif">
Your Shopping Cart
</font></p>
<p><font size="2" face ="Verdana, Arial, Helvetica, sans-serif">
This page allows you to modify or empty your shopping cart contents.
Simply change the number of each product you wish to purchase and
select the "Update cart" link at the bottom.</font></p>
<form name="Update" method="post" action="/cart.php">
<input type="hidden" name="req" value="update">
<table width="90%" border="1" cellspacing="0" cellpadding="4" align="center">
<tr>
<td>Qty</td>
<td>Product</td>
<td align="right">Price</td>
<td align="right">Product Total</td>
</tr>

<?php

$total = mysql_result(mysql_query("SELECT sum(product_qty * product_price) AS subtotal FROM shopping_carts WHERE cart_identifier='$cart_id'"),0);

$total = number_format($total, 2);

$sql = mysql_query("SELECT * FROM shopping_carts WHERE cart_identifier='$cart_id'");

while($row = mysql_fetch_array($sql)) {
$product_total = number_format(($row[product_qty] * $row[product_price]),2);
echo "<tr>" . "<td>" . "<input type=\"text\" " . "name=\"qty[$row[product_id]]\" " . "size=\"2\" value=\"$row[product_qty]\">" . "<br /><font size=\"2\">" .
     "<a href=\"/cart.php?req=remove&" . "product_id=$row[product_id]\">" . "Remove</a>" . 
     "</td>" . "<td><a href=\"/products.php?req=view&" . "product_id=$row[product_id]\">" . stripslahes($row[product_title]) . "</a></td>" .
     "<td align=\"right\">\$$row[product_price]</td>" . "<td align=\"right\">\$$product_total</td>" . "</tr>";
}
?>

<tr>
<td colspan="2"> </td>
<td align="right">Total:</td>
<td> align="right">$<?=$total?></td>
</tr>
<tr>
<td colspan="4" align="center">
<a href="javascript:void(document.update.submit())">Update Cart</a>
  |  
<a href="/cart.php?req=empty_confirm">Empty Cart</a>
  |  
<a href="/products.php">Continue Shopping</a>
  |  
<a href="/checkout.php">Checkout</a>
</td>
</tr>
</table>
</form>

<?php
footer();
break;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.