zmieniłem na
$action =isset($_GET['action']);
i wywala mi błąd: Notice: Undefined index: id in C:\xampp\htdocs\Myonlinestore\card.php on line 17
i po odswieżeniu dodaje mi jeden w
$cart = $_SESSION['cart'];
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$s = (count($items) > 1) ?
's':''; return '<p>You have'.count($items).' item'.$s.' in your shopping cart</p>'; }
tu jest cały kod
card.php
<?php
?>
<?php
// Include database connection
require_once('storesript/conecttomysql.php');
// Include functions
require_once('functions.inc.php');
// Start the session
$cart = $_SESSION['cart'];
$action =isset($_GET['action']); switch ($action) {
case 'add':
if ($cart) {
$cart .= ','.$_GET['id'];
} else {
$cart = $_GET['id'];
}
break;
case 'delete':
if ($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) {
$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="style/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
?>
<p><a href="products.php">Back to bookshop...</a></p>
</div>
</body>
</html>
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
$s = (count($items) > 1) ?
's':''; return '<p>You have'.count($items).' item'.$s.' in your shopping cart</p>'; }
}
function showCart() {
$cart = $_SESSION['cart'];
if ($cart) {
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ?
$contents[$item] + 1
: 1; }
$output[] = '<form action="card.php?action=update" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1"); $productname= $row ["productname"];
$producent= $row ["producent"];
$cena= $row ["cena"];
$output[] = '<tr>';
$output[] = '<td><a href="card.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$productname.' by '.$producent.'</td>';
$output[] = '<td>£'.$cena.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>£'.($cena * $qty).'</td>';
$total += $cena * $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>';
}
}
?>