id i indkøbskurv
Den indkøbskurv jeg har tilpasset mit site har et (tror jeg nok) Hver gang man tilføjer et nyt produkt laver den et nyt id i "cart" databasen.Det er vel ikke meningen at hver gang en kunde tilføjer et nyt produkt til indkøbskurven så laver den nyt id. dvs. at den samme kunne i "cart" tabellen i DB´en får flere id?
Hold på hat og briller her kommer scriptet:
DELETE FROM `cart` WHERE `cartId` = '11' LIMIT 1;
<?php
include("db.php");
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
function AddItem($itemId, $qty)
{
// Will check whether or not this item
// already exists in the cart table.
// If it does, the UpdateItem function
// will be called instead
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
// Check if this item already exists in the users cart table
$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
$row = mysql_fetch_row($result);
$numRows = $row[0];
if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query
@mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead
UpdateItem($itemId, $qty);
}
}
function UpdateItem($itemId, $qty)
{
// Updates the quantity of an item in the users cart.
// If the qutnaity is zero, then RemoveItem will be
// called instead
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
if($qty == 0)
{
// Remove the item from the users cart
RemoveItem($itemId);
}
else
{
mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}
}
function RemoveItem($itemId)
{
// Uses an SQL delete statement to remove an item from
// the users cart
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}
function ShowCart()
{
// Gets each item from the cart table and display them in
// a tabulated format, as well as a final total for the cart
global $dbServer, $dbUser, $dbPass, $dbName;
// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$totalCost = 0;
$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");
?>
<html>
<head>
<title>Jernbanemaerker.dk - Indkøbskurv</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/styles.css" rel="stylesheet" type="text/css">
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
}
</script>
<style type="text/css">
<!--
.style2 {color: #89744B}
.style5 {font-size: 11px}
-->
</style>
<script language="javascript" type="text/javascript">
window.onload = function(){update();setInterval('update()',60000)}
function update(){
var d = new Date();
document.getElementById('minSpan').innerHTML = d.format("hh:uu | \\d. d. mmmm");
}
</script>
<script language="javascript" type="text/javascript" src="roenvingDate.js"></script>
</head>
<body bgcolor="#D8C5A3">
<table width="900" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="right" valign="top">
<td width="900" height="143" class="top"><table width="555" height="138" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left" valign="bottom" class="style2"><p class="style2 style5"><?php include("menu.php"); ?></td>
</tr>
</table></td>
</tr>
<tr>
<td height="251" valign="top" class="body" bgcolor="#F2E3C8"><p> </p>
<form name="frmCart" method="get">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr class="text">
<td height="12"> </td>
<td width="37%" height="12">PRODUKT</td>
<td width="16%">VARETYPE</td>
<td height="12">ANTAL</td>
<td height="12" colspan="2" align="right">PRIS PR. STK.</td>
<td height="12"> </td>
<td height="12">SLET VAREN?</td>
</tr>
<tr class="text">
<td height="13" colspan="8"><img src="gfx/prik.jpg" width="100%" height="1"></td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
<tr class="text">
<td width="2%" height="25"> </td>
<td class="text"><?php echo $row["itemName"]; ?></td>
<td class="text"><?php echo str_replace("_", " ", $row['itemType']); ?></td>
<td width="14%" height="25" class="text">
<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
<?php
//for($i = 1; $i <= 20; $i++)
for($i = 1; $i <= $row["itemQty"]; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</td>
<td width="4%" height="25" align="right">
KR.
</td>
<td width="7%" align="right"><?php echo " ".number_format($row["itemPrice"], 2, ",", ","); ?></td>
<td width="8%"> </td>
<td width="12%" height="25">
<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>" class="style2">SLET VAREN</a>
</td>
</tr>
<?php
}
// Display the total
?>
<tr class="text">
<td colspan="8"><img src="gfx/prik.jpg" width="100%" height="1"> </td>
</tr>
<tr class="text">
<td> </td>
<td colspan="2"> </td>
<td align="right">TOTAL: </td>
<td align="right"> KR.</td>
<td align="right"><b><?php echo " ".number_format($totalCost, 2, ",", "."); ?></b></td>
<td> </td>
<td> </td>
</tr>
<tr class="text">
<td> </td>
<td colspan="2"> </td>
<td align="right"> </td>
<td colspan="4"> </td>
</tr>
<tr class="text">
<td> </td>
<td colspan="2">+ Porto i henhold til Post Danmark </td>
<td align="right"> </td>
<td align="right">KR.</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr class="text">
<td> </td>
<td colspan="2"> </td>
<td align="right"> </td>
<td colspan="4"> </td>
</tr>
<tr class="text">
<td> </td>
<td colspan="2"><a href="java script:history.go(-1);" class="style2"><< Keep Shopping</a></td>
<td align="right"> </td>
<td colspan="4"><a href="bestil.php">Send bestilling >></a>
</td>
</tr>
</table>
</form>
</p></td>
</tr>
<tr>
<td height="252" align="right" valign="bottom" bgcolor="#F2E3C8" class="body"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="3" height="2" bgcolor="#D8C5A3"></td>
</tr>
<tr>
<td width="16%" align="right" id="dato" title="Dato" class="style2"><p><p class="style2 style5"><br>
<span id="minSpan"></span></p></td>
<td width="83%" align="right" class="style2"><p class="style2 style5"><br>
Design og programmering af Klaus Nissen, <a href="http://www.triodimenzionale.dk" target="_blank">www.Triodimenzionale.dk</a></p></td>
<td width="1%" align="right"> </td>
</tr>
<tr>
<td align="right" valign="middle" id="dato" title="Dato" class="style2"> </td>
<td align="right" class="style2"> </td>
<td align="right"> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
<?php
}
?>
