tjeck antal i databasen og vis på sitet?
Hej eksperterJeg bruger følgende script i min indkøbskurv, i scriptet kunne jeg godt tænke mig at den går ind i databasen og tjecker hvor mange der er af hver item og skriver det antal på sitet i "antal" drop down menuen.
Det vil sige hvis der kun er 3 kan man vælge 1-3 styk af den givne vare:
<?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>
</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"><a href="#">FORSIDE</a> | <a href="#">PRODUKTER</a> | <a href="#">KONTAKT</a> | <a href="#" class="style2">INDKØBSKURV</a></td>
</tr>
</table></td>
</tr>
<tr>
<td height="503" 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">PRIS PR. STK.</td>
<td height="12">SLET VAREN?</td>
</tr>
<tr class="text">
<td height="13" colspan="6"><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 $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++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</td>
<td width="19%" height="25">
KR.<?php echo " ".number_format($row["itemPrice"], 2, ".", ","); ?>
</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="6"><img src="gfx/prik.jpg" width="100%" height="1"> </td>
</tr>
<tr class="text">
<td> </td>
<td colspan="2"><a href="products.php" class="style2"><< Keep Shopping</a></td>
<td align="right">TOTAL: </td>
<td colspan="2"> KR.<b><?php echo " ".number_format($totalCost, 2, ".", ","); ?></b></td>
</tr>
</table>
</form>
</p></td>
</tr>
</table>
</body>
</html>
<?php
}
?>
Håber der er nogen der kan hjælpe mig med problemet...
/Snig
