Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

showCart() not working

Status
Not open for further replies.

tshey

Technical User
Dec 28, 2005
78
AU
I have a page that displays products from a database. I have the functions for the cart on an external page. Products page as follows:
<?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();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
<html xmlns=" xml:lang="en" lang="en">
<head>
<title>Catalogue</title>
<link rel="stylesheet" href="css/styles.css" />
</head>

<body>

<div id="shoppingcart">

<h1>Your Shopping Cart</h1>

<?php
echo writeShoppingCart();
?>

</div>

<div id="vacList">

<h1>Our Store </h1>

<?php

$sql = 'SELECT * FROM store_items ORDER BY id';
$result = $db->query($sql);
$output[] = '<ul>';
while ($row = $result->fetch()) {
$output[] = '<li>"'.$row['item_title'].'" desc '.$row['item_desc'].': $'.$row['item_price'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
}
$output[] = '</ul>';
echo join('',$output);
?>

</div>

</body>
</html>

It displays on the page from the database but does not parse the selected item to the cart.php page. Do you need the functions script?
 
Can someone please help me? The database is store_items:
mysql> create table store_items (
-> id int not null primary key auto_increment,
-> cat_id int not null,
-> item_title varchar (100),
-> item_price float (8,2),
-> item_desc text,
-> item_image varchar (50)
-> );
Query OK, 0 rows affected (0.01 sec)
 
HEEEELLLLP!
The functions is:
<?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 store_items 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_title.' by '.$item_desc.'</td>';
$output[] = '<td>&$;'.$item_price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>&$;'.($item_price * $qty).'</td>';
$total += $item_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);
}
?>
 
is this a downloaded script from a site?? if so, they should have a support section on their website

Regards,

Martin

Computing Help And Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top