Hele koden:
<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return 'Indkøbskurven er tom';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return 'Du har <a href="cart.php">'.count($items).' ting'.$s.' i din indkøbskurv</a>';
}
}
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 border=1 cellspacing=0 borderColor=black>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM toej WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td width=30px><a href="cart.php?action=delete&id='.$id.'" class="r">Slet</a></td>';
$output[] = '<td width=60px>'.$del.'</td>';
$output[] = '<td width=200px>Mærke:<br /> '.$maerke.'</td>';
$output[] = '<td width=90px>Kr:'.$pris = number_format($pris, 2, ",", ".").'</td>';
$output[] = '<td width=50px><input type="text" name="qty'.$id.'" value="'.$qty.'" size="2" maxlength="2" /></td>';
$output[] = '<td width=100px>Kr:'.number_format($pris * $qty, 2, ",", ".").'</td>';
$total += $pris * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<br />I alt: <strong>Kr:'.$total = number_format($total, 2, ",", ".").'</strong>';
$output[] = '<div><br /><button type="submit">Opdater kurv</button></div>';
$output[] = '</form>';
} else {
$output[] = 'Din indkøbskurv er tom.';
}
return join('',$output);
}
?>
og min tabel:
CREATE TABLE `toej` (
`id` int(6) unsigned NOT NULL auto_increment,
`del` varchar(100) NOT NULL default '',
`pic` text NOT NULL,
`maerke` varchar(100) NOT NULL default '',
`pris` decimal(18,2) NOT NULL default '0.00',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
og min cart.php:
<?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();
// 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>Indkøbskurv2</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
<style type="text/css">
<!--
.style1 {
font-size: 18px;
font-weight: bold;
}
-->
</style>
</head>
<body>
<span class="style1">Din indkøbskurv </span><br />
<br />
<table width="250" border="1" cellpadding="2" cellspacing="0" bordercolor="#000000">
<tr>
<td><?php
echo writeShoppingCart();
?></td>
</tr>
</table>
<br />
<br />
<span class="style1">Tjek antal...</span><br />
<br />
<?php
setlocale(LC_ALL, 'da_DK');
echo showCart();
?>
<a href="index2.php">Tilbage til forretning...</a>
</body>
</html>