Valg af samme vare 2 gange i indkøbskurv
Hej alle I eksperter.Jeg har fundet et script til en indkøbskurv, som fungerer fint.
Men jeg har et ønske til en ændring og ved ikke hvordan jeg realiserer det.
Jeg har brug for at kunne tilføje den samme vare flere gange, da den fåes i forskellige størrelser og farver. Disse valg skal foretages i selve indkøbskurven.
Jeg kan fint ændre så varen optages i kurven flere gange, men opdateringen af antal påvirker alle de varelinier, hvori varen indgår.
Nogle der kan hjælpe med dette???
Herunder relevante sider fra scriptet.
produkter.php:
<?php
include("includes/db.php");
include("includes/functions.php");
if(isset($_REQUEST['command']) && $_REQUEST['command']=='add' && $_REQUEST['productid']>0){
$pid=$_REQUEST['productid'];
addtocart($pid,1);
header("location:kurv.php");
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="shopping.css" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Produkter</title>
<script language="javascript">
function addtocart(pid){
document.form1.productid.value=pid;
document.form1.command.value='add';
document.form1.submit();
}
</script>
</head>
<body>
<form name="form1">
<input type="hidden" name="productid" />
<input type="hidden" name="command" />
</form>
<div align="center">
<h1>Produkter</h1>
<table id="produkttabel">
<?php
$result=mysql_query("select * from products") or die("select * from products"."<br/><br/>".mysql_error());
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><img src="<?php echo $row['picture']?>" /></td>
<td> <b><?php echo $row['name']?></b><br />
<?php echo $row['description']?><br />
Pris:<big style="color:green"> kr. <?php echo $row['price']?></big><br /><br />
<input type="button" value="Læg i kurv" onclick="addtocart(<?php echo $row['serial']?>)" />
</td>
</tr>
<tr><td colspan="2"><hr size="1" /></td>
<?php } ?>
</table>
</div>
</body>
</html>
kurv.php:
<?php
include("includes/db.php");
include("includes/functions.php");
if(isset($_REQUEST['command']) && $_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
remove_product($_REQUEST['pid']);
}
else if(isset($_REQUEST['command']) && $_REQUEST['command']=='clear'){
unset($_SESSION['cart']);
}
else if(isset($_REQUEST['command']) && $_REQUEST['command']=='update'){
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){
$pid=$_SESSION['cart'][$i]['productid'];
$q=intval($_REQUEST['product'.$pid]);
if($q>0 && $q<=999){
$_SESSION['cart'][$i]['qty']=$q;
}
else{
$msg='Nogle af produkterne er ikke opdateret! Antallet skal være mellem 1 og 999';
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="shopping.css" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Shopping Cart</title>
<script language="javascript">
function del(pid){
if(confirm('Er du sikker på, at du vil slette denne vare?')){
document.form1.pid.value=pid;
document.form1.command.value='delete';
document.form1.submit();
}
}
function clear_cart(){
if(confirm('Dette vil tømme din kurv. Vil du fortsætte?')){
document.form1.command.value='clear';
document.form1.submit();
}
}
function update_cart(){
document.form1.command.value='update';
document.form1.submit();
}
</script>
</head>
<body>
<form name="form1" method="post">
<input type="hidden" name="pid" />
<input type="hidden" name="command" />
<div id="tabel">
<div id="flyt_tabel">
<h1>Din indkøbskurv</h1>
<input type="button" value="Fortsæt i shoppen" onclick="window.location='produkter.php'" />
</div>
<table id="tabel_style">
<?php
if(is_array($_SESSION['cart'])){
echo '<tr style="font-weight:bold"><td>Varelinie</td><td>Navn</td><td>Pris</td><td>Antal</td><td>Beløb</td><td>Slet</td></tr>';
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){
$pid=$_SESSION['cart'][$i]['productid'];
$q=$_SESSION['cart'][$i]['qty'];
$pname=get_product_name($pid);
if($q==0) continue;
?>
<tr bgcolor="#FFFFFF"><td><?php echo $i+1?></td><td><?php echo $pname?></td>
<td>kr <?php echo get_price($pid)?></td>
<td><input type="text" title="Husk at klikke på 'Opdatèr kurven'" name="product<?php echo $pid?>" value="<?php echo $q ?>" maxlength="3" size="2" /></td>
<td>kr <?php echo get_price($pid)*$q?></td>
<td><a href="java script:del(<?php echo $pid?>)">Slet varen</a></td></tr>
<?php
}
?>
<tr><td><b>Ordretotal kr: <?php echo get_order_total()?>,-</b></td><td colspan="5" align="right"><input type="button" value="Tøm kurven" onclick="clear_cart()"><input type="button" value="Opdatèr kurven" onclick="update_cart()"><input type="button" value="Bekræft ordre" onclick="window.location='fakturering.php'"></td></tr>
<?php
}
else{
echo "<tr bgColor='#FFFFFF'><td>Din indkøbskurv er tom!</td>";
}
?>
</table>
</div>
</form>
</body>
</html>
og funktioner.php:
<?php
function get_product_name($pid){
$result=mysql_query("select name from products where serial=$pid") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
$row=mysql_fetch_array($result);
return $row['name'];
}
function get_price($pid){
$result=mysql_query("select price from products where serial=$pid") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
$row=mysql_fetch_array($result);
return $row['price'];
}
function remove_product($pid){
$pid=intval($pid);
$max=count($_SESSION['cart']);
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['cart'][$i]['productid']){
unset($_SESSION['cart'][$i]);
break;
}
}
$_SESSION['cart']=array_values($_SESSION['cart']);
}
function get_order_total(){
$max=count($_SESSION['cart']);
$sum=0;
for($i=0;$i<$max;$i++){
$pid=$_SESSION['cart'][$i]['productid'];
$q=$_SESSION['cart'][$i]['qty'];
$price=get_price($pid);
$sum+=$price*$q;
}
return $sum;
}
function addtocart($pid,$q){
if($pid<1 or $q<1) return;
if(is_array($_SESSION['cart'])){
if(product_exists($pid)) return;
$max=count($_SESSION['cart']);
$_SESSION['cart'][$max]['productid']=$pid;
$_SESSION['cart'][$max]['qty']=$q;
}
else{
$_SESSION['cart']=array();
$_SESSION['cart'][0]['productid']=$pid;
$_SESSION['cart'][0]['qty']=$q;
}
}
function product_exists($pid){
$pid=intval($pid);
$max=count($_SESSION['cart']);
$flag=0;
for($i=0;$i<$max;$i++){
if($pid==$_SESSION['cart'][$i]['productid']){
$flag=1;
break;
}
}
return $flag;
}
?>