Hi guys,
I am currently in the process of making a shopping cart by following this tutorial - http://v3.thewatchmakerproject.com/journal/276/ . I am getting two errors when I follow this tutorial - Fatal error: Call to a member function query() on a non-object in includes/functions.inc.php on line 27 and - Undefined index: action. I have attached my code and if you could provide any help or feedback that would be greatly appreciated.
functions.inc.php:
cart.php:
I am currently in the process of making a shopping cart by following this tutorial - http://v3.thewatchmakerproject.com/journal/276/ . I am getting two errors when I follow this tutorial - Fatal error: Call to a member function query() on a non-object in includes/functions.inc.php on line 27 and - Undefined index: action. I have attached my code and if you could provide any help or feedback that would be greatly appreciated.
functions.inc.php:
<?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="index.php?p=cart">'.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="index.php?p=cart&?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM products WHERE prod_id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="index.php?p=cart&?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$product_name.' - '.$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[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
?>
cart.php:
<?php
if(!isset($_SESSION['username'])) {
echo "Welcome Guest!";
} else {
echo "<font color='red'>You are logged in as </font><font color='purple'>$_SESSION[username].</font><font color='orange'><a href='index.php?p=logout'> logout?</a> </font><br><font color='red'>Dont forget $_SESSION[username]. always feel free to contact us for support when needed most</font><br><br>";
}
?>
<?php # main.inc.php
/*
* This is the main content module.
* This page is included by index.php.
*/
// Redirect if this page was accessed directly:
if (!defined('BASE_URL')) {
// Need the BASE_URL, defined in the config file:
require_once ('../includes/config.inc.php');
// Redirect to the index page:
$url = BASE_URL . 'index.php';
header ("Location: $url");
exit;
} // End of defined() IF.
// Include functions
require_once('includes/functions.inc.php');
// Start the session
// 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;
?>
<h2>Your Shopping Cart</h2>
<?php echo writeShoppingCart(); ?>
<h2>Please Check Quantities</h2>
<p>Please check all the products with their quanitites and total and then please go to the checkout to buy your products</p>
<hr />
<div id="cartdisplay">
<?php echo showCart(); ?>
</div>
<hr />
<p><a href="index.php">Back to Home Page!</a></p>