Page 1 of 1
empty cart function
Posted: Sun Nov 14, 2004 10:17 pm
by C_Calav
hi guys. im getting a bit stuck at making a empty cart function where i delete all items out of the cart for that user.
am i sending the right info? im not sure which attribute to use for the user.
if anyone could help me out that would be great!
thanx, Chris
here is my cart table
[mysql_man]
create table cart
(
cartId int auto_increment not null,
cookieId varchar(50),
itemId int,
qty int,
primary key(cartId),
unique id(cartId)
);
[/mysql_man]
here are my functions
Code: Select all
<?php
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($_GET["id"]);
ShowCart($rs_cart);
break;
}
default:
{
ShowCart($rs_cart);
}
}
?>
Code: Select all
<?php
function RemoveItem($itemId)
{
mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND itemId = $itemId");
}
function EmptyCart($itemId)
{
mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() ."'");
}
?>
here is my button
Code: Select all
<input type="submit" value="Reset" onclick="form.action='cart.php?action=empty_cart'" class="button" />
Suggestion
Posted: Mon Nov 15, 2004 1:47 am
by thomas777neo
Regarding your code:
Code: Select all
function EmptyCart($itemId) { mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() ."'"); }
Shouldn't it look something like this:
Code: Select all
function EmptyCart($itemId) { mysql_query("DELETE FROM cart WHERE cookieId = '$itemId'"); }
Where you you get the value for
.
I think your problem lies here.
Posted: Mon Nov 15, 2004 3:35 am
by C_Calav
thanx thomas777neo for your help. i have changed the code accordingly. was that all i needed to change? it still does nothing when i click the button.
also i do not understand what you mean by this though:
Where you you get the value for Code:
cookieId = '" . GetCartId() ."'
.
thanx, Chris
function looks like this now:
Code: Select all
function EmptyCart($itemId)
{
mysql_query("DELETE FROM cart WHERE cookieId = '$itemId'");
}
what does your query look like
Posted: Mon Nov 15, 2004 4:25 am
by phpScott
Code: Select all
<?php
function RemoveItem($itemId)
{
$query= "DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND itemId = $itemId";
echo "query is $query<br />";
mysql_query($query);
}
function EmptyCart($itemId)
{
$query="DELETE FROM cart WHERE cookieId = '" . GetCartId() ."'";
echo "query is $query<br />";
mysql_query($query);
}
?>
to see what your query is and to make sure it is correct is probably the easest way to make sure your queries are doing what you ask.
do any of your other functions work?
Posted: Mon Nov 15, 2004 1:48 pm
by C_Calav
thanx phpScott, i will try that when i get home! good tip for future coding.
all the other functions work great. i just added that one at the end as i need to empty my cart.
Posted: Mon Nov 15, 2004 5:09 pm
by C_Calav
can someone please help me with what the sql is too delete all from cart for that user?
phpScott: i have used
and it is returing nothing trying it with both querys below.
im not sure weather the correct query is
Code: Select all
mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() ."'");
or
Code: Select all
mysql_query("DELETE FROM cart WHERE cookieId = '$itemId'");
i thought it would be "delete * from cart where cart id = $cartid" or something along these lines.
can anyone help me?
thanx, Chris
Posted: Mon Nov 15, 2004 9:36 pm
by C_Calav
thomas777neo i tried ur help and it does not return anything?
is this the right code to write for the button to go to the empty cart function?
Code: Select all
<input type="submit" value="Reset" onclick="form.action='cart.php?action=empty_cart'" class="button" />
this is what i have so far:
Code: Select all
<?php
function RemoveItem($itemId)
{
// Uses an SQL delete statement to remove an item from
// the users cart
mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND itemId = $itemId");
}
function EmptyCart($itemId)
{
$query="DELETE * FROM cart WHERE cookieId = $cartId";
echo "query is $query<br />";
mysql_query($query);
}
?>
i have
Code: Select all
<?php echo "cartid is $cartId"; <br /> ?>
to check the cartid later on in the page and it is giving me the correct number i need to delete all items from cart.
it does not work anyone see anything wrong?
Posted: Tue Nov 16, 2004 4:43 am
by phpScott
ichange
Code: Select all
<?php
function EmptyCart($itemId)
{
$query="DELETE * FROM cart WHERE cookieId = $cartId";
echo "query is $query<br />";
mysql_query($query);
}
?>
to
Code: Select all
<?php
function EmptyCart($itemId)
{
$query="DELETE * FROM cart WHERE cookieId = $itemId";
echo "query is $query<br />";
mysql_query($query);
}
?>
providing that $itemId is the cookieId that you want to use.
Posted: Tue Nov 16, 2004 1:53 pm
by C_Calav
thanx for the reply phpScott,
but isnt $itemId for the item and $cartId for the cart?
i did <?php echo "cartid is $cartId"; <br /> ?> and this returns the correct cookieId everytime?
im no expert, this is just what i have found. im pretty sure i have tried your way the first time, but will try again when i get home from work.
also, echo "query is $query<br />"; , in your code does not print anything once the button is pressed, it just prints the "query is" then nothing.
your advice is much appreciated!
Posted: Wed Nov 17, 2004 3:11 am
by phpScott
the problem with your EmptyCart() function is that you pass in $itemId then use $cartId in your query.
Do to variable scoping you EmptyCart function has no idea what $cartId is only what $itemId is. You have to change one variable name or the other to what you want.
The other thing is do you have your display_errors=on in your php.ini file because I'm pretty sure that should have come up as a warning.
Posted: Wed Nov 17, 2004 4:39 am
by C_Calav
ah i see, thanx phpScott again, will change tomorow now bit late and got work in morning. Cheers!
Posted: Wed Nov 17, 2004 11:18 pm
by C_Calav
hi,
phpScott, i did what you said and passed $cartId (or use two of the same vairables) into the function and still not work, nothing happening or getting displayed.
can you or anyone see anything else that i am doing wrong?
thanx heaps!
Code: Select all
<?phpsession_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($_GET["id"]);
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
mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND itemId = $itemId");
}
function EmptyCart($cartId)
{
$query="DELETE * FROM cart WHERE cookieId = $cartId";
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
<div align="center">
<form name="cart" 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>
Posted: Thu Nov 18, 2004 3:18 am
by phpScott
explictly set your form method="GET" because that is what you are using to retrieve the variable in your code $_GET['action'];
echo out your action, id and any thing else you are trying to pass to your empty cart function at the top of the page to make sure that you are getting values you are expecting.
then keep echoing them out at every stage to make sure that they are reaching the function correctly. Or even if the function is getting called.
Look closley for typos and capitilization as that can cause things to go off.
Let me know what happens.
Posted: Thu Nov 18, 2004 5:12 am
by C_Calav
awsome. thanx phpScott will get on to it tomorow, got wrk in morning. fixed up those css errors too, thanx.
Posted: Thu Nov 18, 2004 7:11 pm
by C_Calav
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>