hi phpScott,
ok.. i have looked at the code more closey. the sql work as i tested it on mySQL, and i added in the method get.
i also added
Code: Select all
<a href="cart.php?action=empty_cart">-Empty-</a>
this on my webpage. this link works and emptys the cart like i want! so thats a good thing! but putting this code into a button instead of a hyper link
is where its tripping up.
but my problem is, i dont think it is reaching the empty_cart function and im not sure why. all the other functions work.
here is my code comeplete for you or anyone to have a look at. i dont know how to get the button to work and call the function.
thanx guys and thanx phpScott for all your help!
Code: Select all
<?php
session_start();
// This page contains the connection routine for the
// database as well as getting the ID of the cart, etc
$db = mysql_pconnect(***) or die ("Could not connect to database");
if (!$db)
{
echo 'Error: Could not connect to database. Please try again laterrrrrr.';
exit;
}
mysql_select_db('models') or die ("Could not select database!");
function ConnectToDb($server, $user, $pass, $database)
{
// Connect to the database and return
// true/false depending on whether or
// not a connection could be made.
$s = @mysql_connect($server, $user, $pass);
$d = @mysql_select_db($database, $s);
if(!$s || !$d)
return false;
else
return true;
}
function GetCartId()
{
// This function will generate an encrypted string and
// will set it as a cookie using set_cookie. This will
// also be used as the cookieId field in the cart table
if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{
// There is no cookie set. We will set the cookie
// and return the value of the users session ID
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}
$db = mysql_pconnect('db.iserve.net.nz', 'chris', 'cessna') or die ("Could not connect to database");
if (!$db)
{
echo 'Error: Could not connect to database. Please try again laterrrrrr.';
exit;
}
mysql_select_db('models') or die ("Could not select database!");
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart($rs_cart);
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart($rs_cart);
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart($rs_cart);
break;
}
case "empty_cart":
{
EmptyCart();
ShowCart($rs_cart);
break;
}
default:
{
ShowCart($rs_cart);
}
}
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
// Get a connection to the database
// 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
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
$query="DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND itemId = $itemId";
echo "query is $query<br />";
mysql_query($query);
// mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND itemId = $itemId");
}
function EmptyCart()
{
$query="DELETE FROM cart WHERE cookieId = '" . GetCartId() . "'";
echo "query is $query<br />";
mysql_query($query);
}
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
// Get a connection to the database
$totalCost = 0;
$result = mysql_query("select * from cart inner join planes on cart.itemId = planes.itemId where cart.cookieId = '" . GetCartId() . "' order by planes.P_Name asc");
?>
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Model Aircraft</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css" media="all">
@import "/css/cart.css";
</style>
<script type="text/javascript">
<!--
function getWindowHeight() {
var windowHeight = 0;
if (typeof(window.innerHeight) == 'number') {
windowHeight = window.innerHeight;
}
else {
if (document.documentElement && document.documentElement.clientHeight) {
windowHeight = document.documentElement.clientHeight;
}
else {
if (document.body && document.body.clientHeight) {
windowHeight = document.body.clientHeight;
}
}
}
return windowHeight;
}
function setFooter() {
if (document.getElementById) {
var windowHeight = getWindowHeight();
if (windowHeight > 0) {
var contentHeight = document.getElementById('container').offsetHeight;
var footerElement = document.getElementById('footer');
var footerHeight = footerElement.offsetHeight;
if (windowHeight - (contentHeight + footerHeight) >= 0) {
footerElement.style.position = 'relative';
footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
}
else {
footerElement.style.position = 'static';
}
}
}
}
window.onload = function() {
setFooter();
}
window.onresize = function() {
setFooter();
}
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>
<div id="container"><h1>Models</h1>
<!-- Begin Nav center -->
<div id="navcenter">
<ul>
<li><a href="/index.php">Home</a></li>
<li><a href="/about.php">About</a></li>
<li><a href="/contact.php">Contact</a></li>
<li><a href="/examples.php">Examples</a></li>
<li><a href="/cart.php" id="current">Shopping Cart</a></li>
</ul>
</div>
<!-- End Nav Center -->
<!-- Begin LEFT -->
<div id="left">
<!-- Begin navigation -->
<div id="navcontainer">
<ul>
<li><a href="/1st_flight.php">1st Flight</a></li>
<li><a href="/commercial.php">Commercial</a></li>
<li><a href="#">Famous Air Races</a></li>
<li><a href="#">General Aviation</a></li>
<li><a href="#">Helicopters</a></li>
<li><a href="#">Intl Military</a></li>
<li><a href="#">NASA Craft</a></li>
<li><a href="#">Seaplanes</a></li>
<li><a href="#">US Army</a></li>
<li><a href="#">US Coastguard</a></li>
<li><a href="#">US Marine Coprps</a></li>
<li><a href="#">US Navy</a></li>
<li><a href="#">USAF Fighters</a></li>
<li><a href="#">USAF Piston</a></li>
<li><a href="#">Vintage Commercial</a></li>
<li><a href="#">Vintage Transport</a></li>
<li><a href="#">WWI Aircraft</a></li>
<li><a href="#">WWI Bombers</a></li>
<li><a href="#">WWII Fighters</a></li>
</ul>
</div>
<!-- End navigation -->
</div>
<!-- End LEFT -->
<!-- start content -->
<div id="content">
<p class="text">
<table width="500">
<!--DWLayoutTable-->
<tr>
<td width="92" height="40" valign="top"><strong>Aircraft</strong></td>
<td width="105" valign="top"><strong>Name</strong></td>
<td width="57" valign="top"><strong>Price Ea</strong></td>
<td colspan="2" valign="top"><strong>Qty</strong></td>
<td width="65" valign="top"><strong>Sub-Total</strong></td>
<td width="82" valign="top"></td>
</tr>
<!--DWLayoutTable-->
<?
while($row = mysql_fetch_array($result))
{
// Increment the total cost of all planes
$totalCost += ($rowї"qty"] * $rowї"P_Price"]);
?>
<tr>
<td height="64" valign="top"><img src=./pics/<?php echo $rowї"P_Stock"];?>.jpg alt="image of plane" width="82" height="62" class="planeimage" /></td>
<td><?php echo $rowї"P_Name"]; ?></td>
<td>$<?php echo number_format($rowї"P_Price"], 2, ".", ","); ?></td>
<td colspan="2"><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>
$<?php $rowї"subtotal"] = ($rowї"qty"] * $rowї"P_Price"]); echo number_format($rowї"subtotal"], 2, ".", ","); ?>
</td>
<td valign="center"><div align="center"><a href="cart.php?action=remove_item&id=<?php echo $rowї"itemId"]; ?>">Remove</a>
</div></td>
</tr>
<tr>
<td height="21" colspan="7" valign="center"><hr size="1" color="#666666" NOSHADE /></td>
</tr>
<?
} //You have to have this read as php
?>
<!--DWLayoutTable-->
<tr>
<td height="21" colspan="4" valign="top"><a href=1st_flight.php>Continue
Shopping</a></td>
<td width="45" valign="top"><strong>Total: </strong></td>
<td colspan="2" valign="top">$<?php echo number_format($totalCost, 2, ".", ","); ?>
</td>
</tr>
</table>
<?
} //You have to have this read as php
?>
<br />
<?php echo "cartid is $cartId"; ?><br />
<br />
<br />
<div align="center">
<form name="cart" method="GET" action="">
<input type="submit" value="Reset" onclick="form.action='cart.php?action=empty_cart'" class="button" />
<input type="submit" value="Continue" onclick="form.action='details.php'" class="button" />
</form>
</div>
</p>
</div>
<!-- End Content -->
</div>
<!-- Begin Footer -->
<div id="footer">
<h3>copyright &copy; 2004 Model Aircraft</h3>
</div>
<!-- End Footer -->
</body>
</html>